################
#### replication code for Nathan, 2025, ``Explaining Urban Order," World Politics
#### this file does the core data cleaning tasks with the GIS data
#### it is meant to be run in parallel with a python file that calculates the orientation order of each neighborhood
#### extract t-communities from a street grid
#### calculate entropy (orientation order) for them [IN ASSOCIATED PYTHON FILE]
#### identify *when* they were built + distance to boulevards + distance to capital hqs
#### loop over each of the 36 cities
#### outsheet dataset of t-communities for that city
#### note 1: the analyses in the paper all t-communities with less than 10 nodes, as described in the text
#### note 2:  this code was written under R version 4.2.1 and uses deprecated GIS packages (maptools, sp, rgeos, raster) that will only run in older versions of R. these packages only became deprecated during the life cycle of this research project. you must install an earlier R version to run the full code.
#### note 3: this code takes a *very* long time to run, especially for the larger cities (johannesburg, lagos, etc)
#########################################


library(sp)
library(maptools)
library(rgeos)
library(sf)
library(rgdal)
library(igraph)
library(raster)

#set working directory to replication data folder, data_topost subfolder:
setwd("~/Dropbox/UMProjects_20172018/UrbanEnvironment/code/crossnational_cities/replication_data/data_topost/")
rm(list=ls())


#### FRONT MATTER #####
gpkg_labels <- c("nigeria-NGA_gpkg/abuja-2544.gpkg", "nigeria-NGA_gpkg/abuja-2565.gpkg", "ghana-GHA_gpkg/accra-1910.gpkg", "ethiopia-ETH_gpkg/addis_ababa-5134.gpkg", "tanzania-TZA_gpkg/arusha-4800.gpkg", "mali-MLI_gpkg/bamako-1553.gpkg", "mozambique-MOZ_gpkg/beira-4521.gpkg", "gambia-GMB_gpkg/brikama-1458.gpkg", "liberia-LBR_gpkg/buchanan-1533.gpkg", "burundi-BDI_gpkg/bujumbura-4078.gpkg", "south_africa-ZAF_gpkg/cape_town-3268.gpkg", "botswana-BWA_gpkg/gaborone-3587.gpkg", "nigeria-NGA_gpkg/gombe-2932.gpkg", "nigeria-NGA_gpkg/ibadan-2189.gpkg", "south_africa-ZAF_gpkg/johannesburg-3673.gpkg", "uganda-UGA_gpkg/kampala-4427.gpkg", "togo-TGO_gpkg/kara-2026.gpkg", "sudan-SDN_gpkg/khartoum-4335.gpkg", "rwanda-RWA_gpkg/kigali-4172.gpkg", "democratic_republic_of_the_congo-COD_gpkg/kinshasa-3209.gpkg", "nigeria-NGA_gpkg/lagos-2125.gpkg", "angola-AGO_gpkg/luanda-3050.gpkg", "democratic_republic_of_the_congo-COD_gpkg/lubumbashi-3756.gpkg", "equatorial_guinea-GNQ_gpkg/malabo-2762.gpkg", "swaziland-SWZ_gpkg/manzini-4080.gpkg", "mozambique-MOZ_gpkg/maputo-4220.gpkg", "lesotho-LSO_gpkg/maseru-3631.gpkg", "liberia-LBR_gpkg/monrovia-1526.gpkg", "kenya-KEN_gpkg/nairobi-4808.gpkg", "kenya-KEN_gpkg/nakuru-4729.gpkg", "zambia-ZMB_gpkg/ndola-3859.gpkg", "nigeria-NGA_gpkg/oyo-2191.gpkg", "republic_of_congo-COG_gpkg/pointe_noire-2982.gpkg", "south_africa-ZAF_gpkg/port_elizabeth-3505.gpkg", "benin-BEN_gpkg/porto_novo-2101.gpkg", "senegal-SEN_gpkg/touba-1473.gpkg", "namibia-NAM_gpkg/windhoek-3260.gpkg")
#37 length

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

city_labels <- c("abuja-2544", "abuja-2565", "accra-1910", "addis_ababa-5134", "arusha-4800", "bamako-1553", "beira-4521", "brikama-1458", "buchanan-1533", "bujumbura-4078", "cape_town-3268", "gaborone-3587", "gombe-2932", "ibadan-2189", "johannesburg-3673", "kampala-4427", "kara-2026", "khartoum-4335", "kigali-4172", "kinshasa-3209", "lagos-2125", "luanda-3050", "lubumbashi-3756", "malabo-2762", "manzini-4080", "maputo-4220", "maseru-3631", "monrovia-1526", "nairobi-4808", "nakuru-4729", "ndola-3859", "oyo-2191", "pointe_noire-2982", "port_elizabeth-3505", "porto_novo-2101", "touba-1473", "windhoek-3260")

lengths <- NA

#### open main loop, make t communities within each city after making manual corrections to OSM mapping errors

for(j in 1:length(gpkg_labels)){
	
nodes <- readOGR(paste("./boeing_data/geopackage_objects/", gpkg_labels[j], sep=""), "nodes")
class(nodes)
#spatial points dataframe

edges <- readOGR(paste("./boeing_data/geopackage_objects/", gpkg_labels[j], sep=""), "edges")
class(edges)
#spatial lines dataframe

table(edges$highway)
table(edges$tunnel)
table(edges$bridge)


#make the (almost) unique identifier for all the edges that you'll use to look them up:
edges$u_v_combo <- paste(as.character(edges$u), as.character(edges$v), sep="--")
edges$u_v_combo_reverse <- paste(as.character(edges$v), as.character(edges$u), sep="--")

if("tunnel" %in% names(edges)){
edges.sub <- edges[edges$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified") & edges$bridge!="yes" & edges$tunnel!="building_passage" & edges$tunnel!="yes", ]
 } else {
 	if("bridge" %in% names(edges)){
 edges.sub <- edges[edges$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified") & edges$bridge!="yes", ]
 	 } else {
 	edges$bridge <- "no"
  edges.sub <- edges[edges$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified") & edges$bridge!="yes", ]	 	
 	 }
 }
dim(edges.sub) #these are the residential streets that are eligible
edges.sub2 <- edges[!edges$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified"), ]
dim(edges.sub2) #these are all the primary streets you don't want your t-communities to cross


#YOU NEED TO CUT THIS UP INTO SMALLER BITS FOR THE BIGGEST CITIES TO FINISH THE LOOP... 
#TAKES TOO LONG TO COMPUTE OTHERWISE
if(nrow(edges.sub) > 100000){
	edges.sub_partA <- edges.sub[1:round((nrow(edges.sub)/3), digits=0), ]
	overlaysA <- over(edges.sub_partA, edges.sub2)
	edges.sub_partB <- edges.sub[(round((nrow(edges.sub)/3), digits=0) + 1):round(2*(nrow(edges.sub)/3), digits=0), ]
	overlaysB <- over(edges.sub_partB, edges.sub2)
	edges.sub_partC <- edges.sub[(round(2*(nrow(edges.sub)/3), digits=0) + 1):nrow(edges.sub), ]
	overlaysC <- over(edges.sub_partC, edges.sub2)
	overlays <- rbind(overlaysA, overlaysB, overlaysC)
	
} else {
overlays <- over(edges.sub, edges.sub2)
#this is the list of edges.sub by order with the data from edges.sub2
	}
table(is.na(overlays$highway))


edges.sub.cross <- edges.sub[is.na(overlays$highway)==F,]
dim(edges.sub.cross)
osmids <- c(edges.sub.cross$u, edges.sub.cross$v)
length(osmids)
length(unique(osmids))
osmids.todrop <- names(table(osmids)[table(osmids)>=2])
length(osmids.todrop)

u_v_combos.todrop <- edges.sub.cross$u_v_combo[edges.sub.cross$u %in% osmids.todrop | edges.sub.cross$v %in% osmids.todrop ]
u_v_combos_reverse.todrop <- edges.sub.cross$u_v_combo_reverse[edges.sub.cross$u %in% osmids.todrop | edges.sub.cross$v %in% osmids.todrop ]
length(u_v_combos.todrop)
length(u_v_combos.todrop)

#### now switch over to graphml world 
#read in the graphml file. 
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[j], sep=""), format="graphml")
class(gml)
	
#### NEED TO REDO THE MANUAL FIXES HERE TO THE GRAPHML OBJECT AS WELL
#### NOTE: *THIS TAKES A REALLY LONG TIME TO RUN BECAUSE ITS A LONG LOOP*
el <- get.edgelist(gml)
dim(el)

#note: this step takes a really long time
holder <- as.data.frame(matrix(NA, nrow=nrow(el), ncol=2))
colnames(holder) <- c("u", "v")
for(i in 1:nrow(el)){
	holder$u[i] <- V(gml)$id[el[i,1]]
	holder$v[i] <- V(gml)$id[el[i,2]]
}
holder$u_v_combo <- paste(as.character(holder$u), as.character(holder$v), sep="--")
holder$u_v_combo_reverse <- paste(as.character(holder$v), as.character(holder$u),  sep="--")

holder$rnumb <- 1:nrow(holder)

########################
########################
### PATCH IN THE MANUAL FIXES HERE
########################
########################

if(city_labels[j] %in% c("abuja-2544")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5476910332--5476910422"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5415631477--5475798543"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5445183377--5445264219"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse=="5476910332--5476910422"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5415631477--5475798543"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5445183377--5445264219"]] <- "yes"

}


if(city_labels[j] %in% c("abuja-2565")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5444407723--5587367731"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2242650952--2242652324"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2242652163--3001565204"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5461525417--6909965971"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1808426742--6910643490"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1853124030--6299201542"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1878315062--6910643490"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2223533829--2223533832"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394932837--6932713602"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5589646388--5589646954"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394932833--6928350713"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5441556630--5476008011"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5441913226--5754381200"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4883508086--5587227952"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4883496955--7324265379"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1831342850--3001565173"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394932837--5591387743"]] <- "yes"


E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5444407723--5587367731"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2242650952--2242652324"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2242652163--3001565204"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5461525417--6909965971"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1808426742--6910643490"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1853124030--6299201542"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1878315062--6910643490"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2223533829--2223533832"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394932837--6932713602"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5589646388--5589646954"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394932833--6928350713"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5441556630--5476008011"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5441913226--5754381200"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4883508086--5587227952"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4883496955--7324265379"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1831342850--3001565173"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394932837--5591387743"]] <- "yes"

}



if(city_labels[j] %in% c("accra-1910")){
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4458195739--6009202030"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4454083659--5950269212"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3306753063--4454083663"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1269663206--7733733464"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3774932527--5553400740"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4712528383--5967989308"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5620705462--5620705465"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4458196284--4458196405"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4710890718--4710890722"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3342993624--4710890933"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4710918586--4712003319"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4656972456--5996014800"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4725068163--5917529630"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="326376269--326376272"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3474622688--4702128905"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2150866372--2150866383"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3271298739--5941752378"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3313019522--3313019523"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5582996770--5965683082"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5620801708--5995471004"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse=="4458195739--6009202030"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4454083659--5950269212"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3306753063--4454083663"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1269663206--7733733464"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3774932527--5553400740"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4712528383--5967989308"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5620705462--5620705465"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4458196284--4458196405"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4710890718--4710890722"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3342993624--4710890933"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4710918586--4712003319"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4656972456--5996014800"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4725068163--5917529630"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="326376269--326376272"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3474622688--4702128905"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2150866372--2150866383"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3271298739--5941752378"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3313019522--3313019523"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5582996770--5965683082"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5620801708--5995471004"]] <- "yes"
	
}

if(city_labels[j] %in% c("addis_ababa-5134")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2387227111--2734485849"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3663252068--5529650123"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1876590861--2142312196"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2728773139--5529714824"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1766631708--5210042226"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5471911385--5627233582"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="535930222--5459054403"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1683085817--5606489796"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5606113994--5606168001"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2736271491--5539320511"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5527221475--5527221497"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2081017998--5524796048"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5526814096--5529922722"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5529915994--5529922739"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2250794218--5381799533"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2733374545--5579726867"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5541515649--5549737372"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5549737350--5549737354"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5595557623--5595615591"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2102930965--2103892731"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5621266774--5621266777"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2103892742--5593640515"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5621025439--5621025446"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5362434009--5362434011"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5593624776--5601073264"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2216965796--5589648393"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5472452490--5593722789"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2353439919--2353439921"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="612999520--5572346014"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2353446278--5572346544"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2736271094--5539593908"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1985601254--5539320455"]] <- "yes"





E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2387227111--2734485849"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3663252068--5529650123"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1876590861--2142312196"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2728773139--5529714824"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1766631708--5210042226"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5471911385--5627233582"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="535930222--5459054403"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1683085817--5606489796"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5606113994--5606168001"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2736271491--5539320511"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5527221475--5527221497"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2081017998--5524796048"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5526814096--5529922722"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5529915994--5529922739"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2250794218--5381799533"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2733374545--5579726867"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5541515649--5549737372"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5549737350--5549737354"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5595557623--5595615591"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2102930965--2103892731"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5621266774--5621266777"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2103892742--5593640515"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5621025439--5621025446"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5362434009--5362434011"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5593624776--5601073264"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2216965796--5589648393"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5472452490--5593722789"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2353439919--2353439921"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="612999520--5572346014"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2353446278--5572346544"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2736271094--5539593908"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1985601254--5539320455"]] <- "yes"

}


if(city_labels[j] %in% c("arusha-4800")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3278428411--3278429019"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="262113822--3278429019"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2472650953--2482649775"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2472646148--5967664501"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4813682796--4813682804"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2482649775--2482649776"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="8208768484--8208768500"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2482649876--8208753244"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2482649728--2482660872"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3278428411--3278429019"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="262113822--3278429019"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2472650953--2482649775"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2472646148--5967664501"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4813682796--4813682804"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2482649775--2482649776"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="8208768484--8208768500"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2482649876--8208753244"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2482649728--2482660872"]] <- "yes"

}


if(city_labels[j] %in% c("bamako-1553")){

if(!"bridge" %in%  names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4249205094--4254536857"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1060026693--4249194631"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4249347101--4254520603"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1044655074--1046770810"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6295870140--8087627882"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6275152005--8087629541"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1732421905--8027327753"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5354868135--8081273136"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1739261059--5740159318"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1046770938--6067556083"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1046963931--3194872549"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3195014034--4224608090"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4249205094--4254536857"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1060026693--4249194631"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4249347101--4254520603"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1044655074--1046770810"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6295870140--8087627882"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6275152005--8087629541"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1732421905--8027327753"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5354868135--8081273136"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1739261059--5740159318"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1046770938--6067556083"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1046963931--3194872549"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3195014034--4224608090"]] <- "yes"


}


# # if(city_labels[j] %in% c("beira-4521")){
# E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4458195739--6009202030"]] <- "yes"
# }  #beira looks fine


# if(city_labels[j] %in% c("brikama-1458")){
# E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4458195739--6009202030"]] <- "yes"
# } #brikama is fine


# if(city_labels[j] %in% c("buchanan-1533")){
# E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4458195739--6009202030"]] <- "yes"
# } #buchanan is fine


if(city_labels[j] %in% c("bujumbura-4078")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435341087--6278258526"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435516408--6276434981"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2527695338--6274054295"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1382789567--1382789728"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1971686511--1971686514"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2048232789--6276389251"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435341087--6278258526"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435516408--6276434981"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2527695338--6274054295"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1382789567--1382789728"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1971686511--1971686514"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2048232789--6276389251"]] <- "yes"

}


if(city_labels[j] %in% c("cape_town-3268")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="71094299--71095006"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="82697390--82697393"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="82714219--99208099"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="71329052--101065429"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="101065720--101067423"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="116899652--7484733187"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="336639899--336640509"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="336615741--336636684"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="340308146--340310504"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="7150409959--7150446527"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="353162655--353163288"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="353163986--353164253"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="353163704--6924632757"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="353689549--353690995"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3123216414--3180282298"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="362960341--7522115132"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="369736191--369746622"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="419669395--1325589900"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="410492737--3123153731"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1325590086--7479250723"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5238587482--7479250714"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="71094299--71095006"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="82697390--82697393"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="82714219--99208099"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="71329052--101065429"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="101065720--101067423"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="116899652--7484733187"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="336639899--336640509"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="336615741--336636684"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="340308146--340310504"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="7150409959--7150446527"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="353162655--353163288"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="353163986--353164253"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="353163704--6924632757"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="353689549--353690995"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3123216414--3180282298"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="362960341--7522115132"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="369736191--369746622"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="419669395--1325589900"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="410492737--3123153731"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1325590086--7479250723"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5238587482--7479250714"]] <- "yes"


}


if(city_labels[j] %in% c("gaborone-3587")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2620348161--4702132973"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2620348161--4702132973"]] <- "yes"


}


if(city_labels[j] %in% c("gombe-2932")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3966544148--5652584889"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5361600776--5361703973"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5361535304--5361537822"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3969134522--3969134529"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3969069953--3969070588"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3969069953--3969070671"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3969134859--3969069920"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3969134684--3969134738"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3969248194--3969248211"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3966544148--5652584889"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5361600776--5361703973"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5361535304--5361537822"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3969134522--3969134529"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3969069953--3969070588"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3969069953--3969070671"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3969134859--3969069920"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3969134684--3969134738"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3969248194--3969248211"]] <- "yes"


}


if(city_labels[j] %in% c("ibadan-2189")){

if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1376722981--5400329799"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5395116229--5400275590"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5400275590--5400276936"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1387572729--1387572732"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1387572729--1387572732"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5407481041--5407481119"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5406930234--5407612844"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5406972831--5407459223"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5271942794--5406972717"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1374000334--5407438486"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1374000335--1386119580"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1374000335--1386119580"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1377033632--5408768793"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1377033632--5408768793"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108345690--5422194585"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5422194523--5422238756"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5391616588--5391696940"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5391662255--5391662258"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5391662154--5391697286"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394896527--5394896700"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394896511--5394896704"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3932020273--5392504567"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394929134--7140445610"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5394718336--5395000070"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108659260--5430650347"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108659260--5430650347"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108400680--5409683820"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108400648--5409683641"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1106391475--1106392240"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108400649--1108400675"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108271227--1108272509"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1108189907--5430642061"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1115430482--5448306480"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="701170390--5431654033"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5415962971--5431654009"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="701175104--701175153"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="701175114--5429350027"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="701170997--5431686490"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5431686499--701103056"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="701166237--5429438800"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5426939875--5437159162"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="701103317--5422994740"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1105504414--5414589063"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5422704261--5422704312"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5422654888--5422766813"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5424317249--5424317256"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1109176088--5424578290"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1119542313--1119543214"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5482990666--5511632327"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5489155922--5489307502"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5495220307--5499135449"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5436972995--7140522506"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2358567141--5481859392"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5436519789--5436649858"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5436394681--5436587590"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5436490647--5436490655"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5167487661--5487585627"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5167487661--5487585627"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5436587298--5436613222"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3932020242--5436414734"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5423877930--5423877951"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1105743884--1105744030"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1105743369--5396253668"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1112555000--5401387967"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1106356109--5401433784"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1106355377--1106356089"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1106356402--5407728976"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1106357244--5407729049"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5400339959--5400539720"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5397884362--5400539725"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1377666045--5439736632"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5424654236--5424833866"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5424618724--5424799338"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5483171100--5508807035"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5489306600--5489337630"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1118783640--5508145074"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5401571225--5401571230"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5422536531--5426529377"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1116356200--5416739503"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1115565424--5431402827"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5429514123--5431402892"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5429514123--5431402892"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1115494916--5429295595"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5425918456--5427888934"]] <- "yes"


E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1376722981--5400329799"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5395116229--5400275590"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5400275590--5400276936"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1387572729--1387572732"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1387572729--1387572732"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5407481041--5407481119"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5406930234--5407612844"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5406972831--5407459223"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5271942794--5406972717"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1374000334--5407438486"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1374000335--1386119580"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1374000335--1386119580"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1377033632--5408768793"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1377033632--5408768793"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108345690--5422194585"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5422194523--5422238756"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5391616588--5391696940"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5391662255--5391662258"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5391662154--5391697286"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394896527--5394896700"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394896511--5394896704"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3932020273--5392504567"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394929134--7140445610"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5394718336--5395000070"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108659260--5430650347"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108659260--5430650347"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108400680--5409683820"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108400648--5409683641"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1106391475--1106392240"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108400649--1108400675"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108271227--1108272509"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1108189907--5430642061"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1115430482--5448306480"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="701170390--5431654033"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5415962971--5431654009"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="701175104--701175153"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="701175114--5429350027"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="701170997--5431686490"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5431686499--701103056"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="701166237--5429438800"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5426939875--5437159162"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="701103317--5422994740"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1105504414--5414589063"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5422704261--5422704312"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5422654888--5422766813"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5424317249--5424317256"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1109176088--5424578290"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1119542313--1119543214"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5482990666--5511632327"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5489155922--5489307502"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5495220307--5499135449"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5436972995--7140522506"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2358567141--5481859392"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5436519789--5436649858"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5436394681--5436587590"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5436490647--5436490655"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5167487661--5487585627"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5167487661--5487585627"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5436587298--5436613222"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3932020242--5436414734"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5423877930--5423877951"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1105743884--1105744030"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1105743369--5396253668"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1112555000--5401387967"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1106356109--5401433784"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1106355377--1106356089"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1106356402--5407728976"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1106357244--5407729049"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5400339959--5400539720"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5397884362--5400539725"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1377666045--5439736632"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5424654236--5424833866"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5424618724--5424799338"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5483171100--5508807035"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5489306600--5489337630"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1118783640--5508145074"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5401571225--5401571230"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5422536531--5426529377"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1116356200--5416739503"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1115565424--5431402827"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5429514123--5431402892"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5429514123--5431402892"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1115494916--5429295595"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5425918456--5427888934"]] <- "yes"

}

if(city_labels[j] %in% c("johannesburg-3673")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4512470883--5318347292"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="32825298--32825302"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="32825281--32825283"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="32825281--32825152"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2578815153--5966701598"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="259046960--7593662300"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="259847920--5115258261"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="259847922--259847944"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="259847937--260073359"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="259681997--259847923"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="259682007--259847922"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="51602737--51602769"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="340152975--1315886842"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="243969932--6708587842"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="243846569--243846784"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="7060319504--7060319507"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6146877687--6771131258"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="264573146--461645006"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="262258525--7169217128"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5699914403--6880747960"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="365304608--2189066830"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="365504956--7008182129"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="365557083--365609554"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="365559515--5337991713"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="973837778--8137826875"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3542048696--6880116294"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="668982146--6876996147"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2154642874--2150919088"]] <- "yes"	
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4512470883--5318347292"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="32825298--32825302"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="32825281--32825283"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="32825281--32825152"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2578815153--5966701598"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="259046960--7593662300"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="259847920--5115258261"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="259847922--259847944"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="259847937--260073359"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="259681997--259847923"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="259682007--259847922"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="51602737--51602769"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="340152975--1315886842"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="243969932--6708587842"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="243846569--243846784"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="7060319504--7060319507"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6146877687--6771131258"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="264573146--461645006"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="262258525--7169217128"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5699914403--6880747960"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="365304608--2189066830"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="365504956--7008182129"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="365557083--365609554"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="365559515--5337991713"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="973837778--8137826875"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3542048696--6880116294"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="668982146--6876996147"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2154642874--2150919088"]] <- "yes"		
	
}


if(city_labels[j] %in% c("kampala-4427")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="561224994--6198281686"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6229400988--6230166644"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6232771874--6369528237"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6198770969--6201533366"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="577290724--6216604638"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="577290004--3998487498"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="577290500--577290520"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6201214563--6201443164"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4004990405--6227080494"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="8035459671--8035459678"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3843387698--8009970331"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6213184043--6743304276"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6216308054--6216308100"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="558113985--560441628"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6221474120--6221490274"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6225395473--560442238"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="557329142--1897323064"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5508923573--5508841355"]] <- "yes"
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="561224994--6198281686"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6229400988--6230166644"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6232771874--6369528237"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6198770969--6201533366"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="577290724--6216604638"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="577290004--3998487498"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="577290500--577290520"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6201214563--6201443164"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4004990405--6227080494"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="8035459671--8035459678"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3843387698--8009970331"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6213184043--6743304276"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6216308054--6216308100"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="558113985--560441628"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6221474120--6221490274"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6225395473--560442238"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="557329142--1897323064"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5508923573--5508841355"]] <- "yes"
		


}


if(city_labels[j] %in% c("kara-2026")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5508923573--5508841355"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="7310750725--7310750728"]] <- "yes"
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5508923573--5508841355"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="7310750725--7310750728"]] <- "yes"


}

if(city_labels[j] %in% c("khartoum-4335")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5899700477--5899701386"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5849508633--5899701397"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3617076174--1410584968"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1535401726--3622467486"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2463688663--3706172206"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2497210227--5971965599"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2459130692--5972106458"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2497210192--2497210259"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2463689404--5920361981"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5916119462--5929409444"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2427401924--2458071468"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2427260907--2458071464"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2422041613--5969577194"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2422041594--2422041600"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5969348524--5969691505"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2479016463--5966478623"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5896337894--5896974794"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2438710048--2438738845"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2495147625--5971923425"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="8088997957--8088997967"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1782542554--3661992090"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3661047387--3661047395"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1783103658--3663893283"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3454345986--5849221707"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1842026879--5849221718"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2140602446--2140602536"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5974748978--5974749614"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2494664436--2494664444"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5899700477--5899701386"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5849508633--5899701397"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3617076174--1410584968"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1535401726--3622467486"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2463688663--3706172206"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2497210227--5971965599"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2459130692--5972106458"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2497210192--2497210259"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2463689404--5920361981"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5916119462--5929409444"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2427401924--2458071468"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2427260907--2458071464"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2422041613--5969577194"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2422041594--2422041600"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5969348524--5969691505"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2479016463--5966478623"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5896337894--5896974794"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2438710048--2438738845"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2495147625--5971923425"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="8088997957--8088997967"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1782542554--3661992090"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3661047387--3661047395"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1783103658--3663893283"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3454345986--5849221707"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1842026879--5849221718"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2140602446--2140602536"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5974748978--5974749614"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2494664436--2494664444"]] <- "yes"

}


if(city_labels[j] %in% c("kinshasa-3209")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="29568425--435876159"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435724717--435876159"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1234974099--4335554681"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="29533526--29670159"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3076795965--3076795966"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="30026054--4330693218"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4325868535--4325868567"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5531241213--7460744780"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="29621942--29752390"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="29752487--3315839516"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435789174--5542854545"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="30023128--435869990"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="30023138--435911813"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435813029--5653835242"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="616479410--616479411"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435813029--5653835242"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="616479411--5640201189"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5594564399--5594593279"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5594564420--5594564556"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5611426741--5611426753"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5596517046--5611508402"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="30025268--4187112997"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4331164232--4331164412"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888813947--5626775540"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888795519--5604642403"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888178817--5602354359"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888178816--3888194362"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888178640--3891425849"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888178587--4335518674"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3888178816--3888194362"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3306880475--3888730240"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3946285976--5590319446"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="253148802--3924756281"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="435665415--5635621609"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="294757635--5635621614"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="253149108--253149341"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2250981045--5647466033"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3898096131--3955231398"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="29833872--258067465"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4268883475--5580159084"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3928087731--3928088124"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5620168975--5626708173"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3887885169--4562372836"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5611736107--5830370973"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3928087731--3928088124"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3913319848--5607310741"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3898931155--5635508473"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2258378472--5635508481"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3315675298--3893468472"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2258337752--3315670147"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2258337709--3315670146"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2258337670--3315670145"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3315670144--3933098799"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3315670142--4329985826"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4329992091--6909834508"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3315629860--5596628627"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3715220137--3715220153"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="29568425--435876159"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435724717--435876159"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1234974099--4335554681"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="29533526--29670159"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3076795965--3076795966"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="30026054--4330693218"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4325868535--4325868567"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5531241213--7460744780"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="29621942--29752390"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="29752487--3315839516"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435789174--5542854545"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="30023128--435869990"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="30023138--435911813"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435813029--5653835242"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="616479410--616479411"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435813029--5653835242"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="616479411--5640201189"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5594564399--5594593279"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5594564420--5594564556"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5611426741--5611426753"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5596517046--5611508402"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="30025268--4187112997"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4331164232--4331164412"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888813947--5626775540"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888795519--5604642403"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888178817--5602354359"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888178816--3888194362"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888178640--3891425849"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888178587--4335518674"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3888178816--3888194362"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3306880475--3888730240"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3946285976--5590319446"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="253148802--3924756281"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="435665415--5635621609"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="294757635--5635621614"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="253149108--253149341"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2250981045--5647466033"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3898096131--3955231398"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="29833872--258067465"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4268883475--5580159084"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3928087731--3928088124"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5620168975--5626708173"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3887885169--4562372836"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5611736107--5830370973"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3928087731--3928088124"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3913319848--5607310741"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3898931155--5635508473"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2258378472--5635508481"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3315675298--3893468472"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2258337752--3315670147"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2258337709--3315670146"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2258337670--3315670145"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3315670144--3933098799"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3315670142--4329985826"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4329992091--6909834508"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3315629860--5596628627"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3715220137--3715220153"]] <- "yes"

}


if(city_labels[j] %in% c("lagos-2125")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3961000043--4392699205"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4392699224--4401699110"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4392698983--4392698984"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2298457387--4392684215"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3964640187--4392684222"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4448458919--4485464597"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4360777852--5489227453"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2297611454--4349958431"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4206137780--4395358008"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4403529040--4403529044"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4344777038--5480195240"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4338774665--4345980478"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3740073183--6860850287"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1484784005--5603497383"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3961000043--4392699205"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4392699224--4401699110"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4392698983--4392698984"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2298457387--4392684215"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3964640187--4392684222"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4448458919--4485464597"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4360777852--5489227453"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2297611454--4349958431"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4206137780--4395358008"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4403529040--4403529044"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4344777038--5480195240"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4338774665--4345980478"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3740073183--6860850287"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1484784005--5603497383"]] <- "yes"
	
	
	

}

if(city_labels[j] %in% c("luanda-3050")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4544023613--4544023617"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4544023598--5950350350"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4557923284--5950508611"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4557923296--4589944710"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3141792456--4557923716"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4557923783--5956161235"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4557923541--4557923559"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4547320001--4547320297"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5748454645--6590618307"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="818596087--5852858522"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="818595878--3827708701"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5953099564--5953372338"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4554231671--5828387224"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4544023613--4544023617"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4544023598--5950350350"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4557923284--5950508611"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4557923296--4589944710"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3141792456--4557923716"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4557923783--5956161235"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4557923541--4557923559"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4547320001--4547320297"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5748454645--6590618307"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="818596087--5852858522"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="818595878--3827708701"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5953099564--5953372338"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4554231671--5828387224"]] <- "yes"	
	

}


if(city_labels[j] %in% c("lubumbashi-3756")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="382158653--2675560419"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2662956981--5617497433"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2728596461--5621750488"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2728596464--5617497431"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2731400157--5621750463"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2728596466--2731943123"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2675561004--2675561019"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5607113598--5607285320"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2731018884--2731018888"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2672717704--2672717705"]] <- "yes"
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="382158653--2675560419"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2662956981--5617497433"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2728596461--5621750488"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2728596464--5617497431"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2731400157--5621750463"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2728596466--2731943123"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2675561004--2675561019"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5607113598--5607285320"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2731018884--2731018888"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2672717704--2672717705"]] <- "yes"
	

}

# if(city_labels[j] %in% c("malabo-2762")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }


# if(city_labels[j] %in% c("manzini-4080")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }

# if(city_labels[j] %in% c("maputo-4220")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }

if(city_labels[j] %in% c("maseru-3631")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2748481771--3010473265"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3666527086--3673585582"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3666503339--3666503344"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3679546122--3976500786"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3673048158--3976400691"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3679546098--3679546105"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3679546098--3679548042"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2981443156--4165489560"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2981358939--2986282049"]] <- "yes"
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2748481771--3010473265"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3666527086--3673585582"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3666503339--3666503344"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3679546122--3976500786"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3673048158--3976400691"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3679546098--3679546105"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3679546098--3679548042"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2981443156--4165489560"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2981358939--2986282049"]] <- "yes"
		

}


# # if(city_labels[j] %in% c("monrovia-1526")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }





if(city_labels[j] %in% c("nairobi-4808")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1875584855--2491507472"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1875584850--1875584853"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1132279121--4611962973"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6333234773--6333234781"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4611962977--6333234767"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6333038620--6333074678"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6315732303--6333038689"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6163523567--7625271876"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3786097225--3786097244"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3786097466--6383675752"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6315732292--7649564804"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5031056573--6339336203"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6347801022--6349618736"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6349358324--6349358326"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3835961189--3836004087"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3835961187--6344213746"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1401559612--2479451200"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1401403308--6163759968"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6333261477--6333737761"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2083923669--2083923678"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1003845517--7520692889"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1026970471--6349196397"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="4922472527--6349196398"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1941020335--6261813376"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1594490046--1594490059"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1940979954--6347660509"]] <- "yes"

E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1875606418--7590603748"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6309884365--7731286622"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6336752337--6336835367"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6336835369--7732065705"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6332661997--6336752350"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6336752403--7924230606"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6336752428--6336835384"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3836111427--3836158064"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="7733504893--7733504930"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="290888306--290889411"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6163463602--7649554432"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="6385558119--6385829660"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1941020845--6349717434"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1941020736--6352015908"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1941020636--6349986710"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1941020736--6352015908"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="1746362546--6274247595"]] <- "yes"


E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1875584855--2491507472"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1875584850--1875584853"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1132279121--4611962973"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6333234773--6333234781"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4611962977--6333234767"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6333038620--6333074678"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6315732303--6333038689"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6163523567--7625271876"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3786097225--3786097244"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3786097466--6383675752"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6315732292--7649564804"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5031056573--6339336203"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6347801022--6349618736"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6349358324--6349358326"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3835961189--3836004087"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3835961187--6344213746"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1401559612--2479451200"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1401403308--6163759968"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6333261477--6333737761"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2083923669--2083923678"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1003845517--7520692889"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1026970471--6349196397"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="4922472527--6349196398"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1941020335--6261813376"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1594490046--1594490059"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1940979954--6347660509"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1875606418--7590603748"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6309884365--7731286622"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6336752337--6336835367"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6336835369--7732065705"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6332661997--6336752350"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6336752403--7924230606"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6336752428--6336835384"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3836111427--3836158064"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="7733504893--7733504930"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="290888306--290889411"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6163463602--7649554432"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="6385558119--6385829660"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1941020845--6349717434"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1941020736--6352015908"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1941020636--6349986710"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1941020736--6352015908"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="1746362546--6274247595"]] <- "yes"
	
}

# if(city_labels[j] %in% c("nakuru-4729")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }

# if(city_labels[j] %in% c("ndola-3859")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }

if(city_labels[j] %in% c("oyo-2191")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="5279200005--5281027999"]] <- "yes"
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="5279200005--5281027999"]] <- "yes"
	

}

if(city_labels[j] %in% c("pointe_noire-2982")){
	
if(!"bridge" %in% names(edges)){
	E(gml)$bridge <- "no"
	}
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3519082373--3697003040"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="2451410544--3699608139"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3519080666--3697629817"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3697089419--5846469790"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo=="3698472376--3699628833"]] <- "yes"
	
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3519082373--3697003040"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="2451410544--3699608139"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3519080666--3697629817"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3697089419--5846469790"]] <- "yes"
E(gml)$bridge[holder$rnumb[holder$u_v_combo_reverse =="3698472376--3699628833"]] <- "yes"
	

}

# # if(city_labels[j] %in% c("port_elizabeth-3505")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }

# if(city_labels[j] %in% c("porto_novo-2101")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }

# if(city_labels[j] %in% c("touba-1473")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }


# if(city_labels[j] %in% c("windhoek-3260")){
	
# if(!"bridge" %in% names(edges)){
	# E(gml)$bridge <- "no"
	# }
	
	
	
	

# }



#will need another ifelse statement here because of the tunnel thing... 

if(length(E(gml)$tunnel) > 0){
#subgraph to just the non-tunnel / non-bridge residential streets
gml.sub <- subgraph.edges(gml, E(gml)[E(gml)$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified") & E(gml)$bridge!="yes" & E(gml)$tunnel!="building_passage" & E(gml)$tunnel!="yes" ], delete.vertices=TRUE)
} else {
	 if(length(E(gml)$bridge) > 0){
gml.sub <- subgraph.edges(gml, E(gml)[E(gml)$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified") & E(gml)$bridge!="yes"  ], delete.vertices=TRUE)
 } else {
 E(gml)$bridge <- "no"
 gml.sub <- subgraph.edges(gml, E(gml)[E(gml)$highway %in% c("['residential', 'living_street']", "['residential', 'unclassified']", "residential", "unclassified") & E(gml)$bridge!="yes"  ], delete.vertices=TRUE)
	
 }
}

table(E(gml.sub)$highway)
length(osmids.todrop)
V(gml.sub)$id[1:10]
gml.sub.nocross <- induced_subgraph(gml.sub, !V(gml.sub)$id %in% osmids.todrop)
gml.sub
gml.sub.nocross  #this is the object you want to work with and decompose

#look at the clusters:
clust <- clusters(gml.sub.nocross)
clust$no 
summary(clust$csize)
length(clust$csize[clust$csize>4]) #potential t-communities to work with --> below this, they're too small to even define as a t-community
summary(clust$csize[clust$csize>4])

#keep only the ones with 5 or more nodes
gml.sub.list <- decompose.graph(gml.sub.nocross , min.vertices=5)
length(gml.sub.list)


##### NOW IDENTIFY THE EXTRA LARGE ONES, CHECK FOR ERRORS #####

# sizes <- clust$csize[clust$csize>4]
# sizes <- sizes > quantile(sizes, c(0.95)) | sizes > 1000
	# #these are the criteria to check if they're ok: flag ones in top 5% or with more than 1000 nodes
# sizes.mat <- cbind(sizes, 1:length(sizes))
# tocheck <- sizes.mat[sizes.mat[,1]==1,2]

# for(k in 1:length(tocheck) ){
 # tcom1 <- gml.sub.list[[tocheck[k]]]
 # eltcom1 <- get.edgelist(tcom1)
 # holder <- as.data.frame(matrix(NA, nrow=nrow(eltcom1), ncol=2))
 # colnames(holder) <- c("u", "v")
 # for(i in 1:nrow(eltcom1)){
	 # holder$u[i] <- V(tcom1)$id[eltcom1[i,1]]
	 # holder$v[i] <- V(tcom1)$id[eltcom1[i,2]]
 # }

 # holder$u_v_combo <- paste(as.character(holder$u), as.character(holder$v), sep="--")
 # holder$u_v_combo_reverse <- paste(as.character(holder$v), as.character(holder$u),  sep="--")
 # edges.tcom1 <- edges[edges$u_v_combo %in% holder$u_v_combo | edges$u_v_combo %in% holder$u_v_combo_reverse, ]
 # dim(edges.tcom1) #half as many as the other file
 # plot(edges.tcom1)
 # #gCentroid(edges.tcom1)
 # shpname <- paste(city_labels[j], "_tcom_", tocheck[k], ".shp", sep="")
 # writeOGR(edges.tcom1, dsn=paste("./code/test_directory/tcommunities/outliers_tocheck/", shpname, sep=""), driver= "ESRI Shapefile", "edges.tcom1")

# } #k loop closes here

save(gml.sub.list, file=paste("./boeing_data/tcommunities/", city_labels[j], ".Rdata", sep=""))

for(i in 1:length(gml.sub.list)){
 write.csv(V(gml.sub.list[[i]])$id, file=paste("./boeing_data/tcommunities/idlists/", city_labels[j], "_tcom_", i, ".csv", sep=""))
 }

lengths <- c(lengths, length(gml.sub.list))

}#close the j loop

lengths[-1]

#####################################################
#####################################################
#####################################################
#####################################################
#####################################################

####### GOES OVER TO PYTHON HERE ###################

####### MUST RUN: "tcommunities.py" ##################
####### NOTE: must first create own .json file with all the relevant files paths #############
####### NOTE: must set the correct working directory in python on your machine ###############

#####################################################
#####################################################
#####################################################
#####################################################
#####################################################


################### MAKE THE TDAT OBJECTS #################

rm(list=ls())

gpkg_labels <- c("nigeria-NGA_gpkg/abuja-2544.gpkg", "nigeria-NGA_gpkg/abuja-2565.gpkg", "ghana-GHA_gpkg/accra-1910.gpkg", "ethiopia-ETH_gpkg/addis_ababa-5134.gpkg", "tanzania-TZA_gpkg/arusha-4800.gpkg", "mali-MLI_gpkg/bamako-1553.gpkg", "mozambique-MOZ_gpkg/beira-4521.gpkg", "gambia-GMB_gpkg/brikama-1458.gpkg", "liberia-LBR_gpkg/buchanan-1533.gpkg", "burundi-BDI_gpkg/bujumbura-4078.gpkg", "south_africa-ZAF_gpkg/cape_town-3268.gpkg", "botswana-BWA_gpkg/gaborone-3587.gpkg", "nigeria-NGA_gpkg/gombe-2932.gpkg", "nigeria-NGA_gpkg/ibadan-2189.gpkg", "south_africa-ZAF_gpkg/johannesburg-3673.gpkg", "uganda-UGA_gpkg/kampala-4427.gpkg", "togo-TGO_gpkg/kara-2026.gpkg", "sudan-SDN_gpkg/khartoum-4335.gpkg", "rwanda-RWA_gpkg/kigali-4172.gpkg", "democratic_republic_of_the_congo-COD_gpkg/kinshasa-3209.gpkg", "nigeria-NGA_gpkg/lagos-2125.gpkg", "angola-AGO_gpkg/luanda-3050.gpkg", "democratic_republic_of_the_congo-COD_gpkg/lubumbashi-3756.gpkg", "equatorial_guinea-GNQ_gpkg/malabo-2762.gpkg", "swaziland-SWZ_gpkg/manzini-4080.gpkg", "mozambique-MOZ_gpkg/maputo-4220.gpkg", "lesotho-LSO_gpkg/maseru-3631.gpkg", "liberia-LBR_gpkg/monrovia-1526.gpkg", "kenya-KEN_gpkg/nairobi-4808.gpkg", "kenya-KEN_gpkg/nakuru-4729.gpkg", "zambia-ZMB_gpkg/ndola-3859.gpkg", "nigeria-NGA_gpkg/oyo-2191.gpkg", "republic_of_congo-COG_gpkg/pointe_noire-2982.gpkg", "south_africa-ZAF_gpkg/port_elizabeth-3505.gpkg", "benin-BEN_gpkg/porto_novo-2101.gpkg", "senegal-SEN_gpkg/touba-1473.gpkg", "namibia-NAM_gpkg/windhoek-3260.gpkg")
#37 length

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

city_labels <- c("abuja-2544", "abuja-2565", "accra-1910", "addis_ababa-5134", "arusha-4800", "bamako-1553", "beira-4521", "brikama-1458", "buchanan-1533", "bujumbura-4078", "cape_town-3268", "gaborone-3587", "gombe-2932", "ibadan-2189", "johannesburg-3673", "kampala-4427", "kara-2026", "khartoum-4335", "kigali-4172", "kinshasa-3209", "lagos-2125", "luanda-3050", "lubumbashi-3756", "malabo-2762", "manzini-4080", "maputo-4220", "maseru-3631", "monrovia-1526", "nairobi-4808", "nakuru-4729", "ndola-3859", "oyo-2191", "pointe_noire-2982", "port_elizabeth-3505", "porto_novo-2101", "touba-1473", "windhoek-3260")


for(j in 1:length(gpkg_labels)){
	
load(paste("./boeing_data/tcommunities/", city_labels[j], ".Rdata", sep="")	)
	
new <- read.csv(paste("./boeing_data/tcommunities/entropy_stats/", city_labels[j], "_tcom_", 1, ".csv", sep=""), header=F)
holder <- as.data.frame(matrix(NA, nrow=length(gml.sub.list), ncol=nrow(new)))
colnames(holder) <- new[,1]
	
	
for(i in 1:length(gml.sub.list)){
	new <- read.csv(paste("./boeing_data/tcommunities/entropy_stats/", city_labels[j], "_tcom_", i, ".csv", sep=""), header=F)
	holder[i,] <- new[,2]

}

tdat <- holder	
tdat$orientation_order  <- as.numeric(tdat$orientation_order)
tdat$node_count  <- as.numeric(tdat$node_count)
	
save(tdat, file=paste("./boeing_data/tcommunities/", city_labels[j], "_tdat", ".Rdata", sep=""))


}  



################################################################################
######### FINAL STEP: date each t-community to the historical street maps
######## note: this step has to go city by city because the shapefiles vary so much by city... 

##### CITY 1: ABUJA ######
##note complexity for this one only that its split across two files 
##*abuja enters Boeing's data as two discontinuous cities

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/abuja-2544.gpkg", "nodes")
class(nodes)
nodes2 <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/abuja-2565.gpkg", "nodes")
class(nodes2)

edges <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/abuja-2544.gpkg", "edges")
class(edges)

edges2 <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/abuja-2565.gpkg", "edges")
class(edges2)


load(paste("./boeing_data/tcommunities/", "abuja-2565", "_tdat", ".Rdata", sep=""))
tdat2 <- tdat

load(paste("./boeing_data/tcommunities/", "abuja-2544", "_tdat", ".Rdata", sep=""))



#urban extent at 1985
t1985 <- readOGR("./extents/abuja/abuja1985.shp")
class(t1985)
proj4string(t1985)
#t1920 <- spTransform(t1920, CRS(" +proj=longlat +datum=WGS84 +no_defs "))


#urban extent at 1991
t1991 <- readOGR("./extents/abuja/abuja1991.shp")
class(t1991)
proj4string(t1991)
#t1920 <- spTransform(t1920, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 2001
t2001 <- readOGR("./extents/abuja/abuja2001.shp")
class(t2001)
proj4string(t2001)

#urban extent at 2010
t2010 <- readOGR("./extents/abuja/abuja2010.shp")
class(t2010)
proj4string(t2010)


#pre_1985
test <- over(nodes, t1985)
length(test)
dim(nodes)
head(test)
nodes$pre_1985 <- 0
nodes$pre_1985[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1985)

test <- over(nodes2, t1985)
length(test)
dim(nodes2)
head(test)
nodes2$pre_1985 <- 0
nodes2$pre_1985[is.na(test)==F] <- 1
table(test)
table(nodes2$pre_1985)

#plot(t1985)
#plot(nodes2[nodes2$pre_1985==1,], col="green", cex=0.8, add=T)
#plot(nodes[nodes$pre_1985==1,], col="blue", cex=0.8, add=T)

#bw1985to1991
test <- over(nodes, t1991)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1991 <- 0
nodes$bw1985to1991[is.na(test$id)==F & nodes$pre_1985 ==0] <- 1
table(nodes$pre_1985, nodes$bw1985to1991)

test <- over(nodes2, t1991)
dim(test)
dim(nodes2)
head(test)
nodes2$bw1985to1991 <- 0
nodes2$bw1985to1991[is.na(test$id)==F & nodes2$pre_1985 ==0] <- 1
table(nodes2$pre_1985, nodes2$bw1985to1991)

#bw1991to2001
test <- over(nodes, t2001)
dim(test)
dim(nodes)
head(test)
nodes$bw1991to2001 <- 0
nodes$bw1991to2001[is.na(test$id)==F & nodes$pre_1985 ==0 & nodes$bw1985to1991 ==0] <- 1
table(nodes$bw1985to1991, nodes$bw1991to2001)

test <- over(nodes2, t2001)
dim(test)
dim(nodes2)
head(test)
nodes2$bw1991to2001 <- 0
nodes2$bw1991to2001[is.na(test$id)==F & nodes2$pre_1985 ==0 & nodes2$bw1985to1991 ==0] <- 1
table(nodes2$bw1985to1991, nodes2$bw1991to2001)

#bw2001to2010
test <- over(nodes, t2010)
dim(test)
dim(nodes)
head(test)
nodes$bw2001to2010 <- 0
nodes$bw2001to2010[is.na(test$id)==F & nodes$pre_1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001 ==0] <- 1
table(nodes$bw1991to2001, nodes$bw2001to2010)

test <- over(nodes2, t2010)
dim(test)
dim(nodes2)
head(test)
nodes2$bw2001to2010 <- 0
nodes2$bw2001to2010[is.na(test$id)==F & nodes2$pre_1985 ==0 & nodes2$bw1985to1991 ==0 & nodes2$bw1991to2001 ==0] <- 1
table(nodes2$bw1991to2001, nodes2$bw2001to2010)

#bw2010to2020
nodes$bw2010to2020 <- 0
nodes$bw2010to2020[ nodes$pre_1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001 ==0 & nodes$bw2001to2010 ==0] <- 1

nodes2$bw2010to2020 <- 0
nodes2$bw2010to2020[ nodes2$pre_1985 ==0 & nodes2$bw1985to1991 ==0 & nodes2$bw1991to2001 ==0 & nodes2$bw2001to2010 ==0] <- 1

#check you've done it right: this statement must be TRUE
(table(nodes$pre_1985)[2] + table(nodes$bw1985to1991)[2] + table(nodes$bw1991to2001)[2]  +table(nodes$bw2001to2010)[2]  +table(nodes$bw2010to2020)[2] ) == nrow(nodes)

(table(nodes2$pre_1985)[2] + table(nodes2$bw1985to1991)[2] + table(nodes2$bw1991to2001)[2]  +table(nodes2$bw2001to2010)[2]  +table(nodes2$bw2010to2020)[2] ) == nrow(nodes2)


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1985 <- NA
tdat$bw1985to1991 <- NA
tdat$bw1991to2001 <- NA
tdat$bw2001to2010 <- NA
tdat$bw2010to2020 <- NA

load(paste("./boeing_data/tcommunities/", "abuja-2544", ".Rdata", sep="")	)

for(i in 1:nrow(tdat)){
	tdat$pre_1985[i] <- mean(nodes@data$pre_1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1991[i] <- mean(nodes@data$bw1985to1991[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1991to2001[i] <- mean(nodes@data$bw1991to2001[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2001to2010[i] <- mean(nodes@data$bw2001to2010[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2010to2020[i] <- mean(nodes@data$bw2010to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

#the historical anchor has to be picked separately for each city based on case knowledge
#for abuja, you're picking: 1977
historical_anchor <- 1977
tdat$average_age <- ((historical_anchor + ((1985 - historical_anchor) / 2)) * tdat$pre_1985 ) + 
((1985 + ((1991 - 1985) / 2)) * tdat$bw1985to1991 ) + 
((1991 + ((2001 - 1991) / 2)) * tdat$bw1991to2001 ) + 
((2001 + ((2010 - 2001) / 2)) * tdat$bw2001to2010 ) + 
((2010 + ((2020 - 2010) / 2)) * tdat$bw2010to2020 ) 
summary(tdat$average_age)

#now repeat for nodes2 / tdat2 ... 
tdat2$pre_1985 <- NA
tdat2$bw1985to1991 <- NA
tdat2$bw1991to2001 <- NA
tdat2$bw2001to2010 <- NA
tdat2$bw2010to2020 <- NA

load(paste("./boeing_data/tcommunities/", "abuja-2565", ".Rdata", sep="")	)

for(i in 1:nrow(tdat2)){
	tdat2$pre_1985[i] <- mean(nodes2@data$pre_1985[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat2$bw1985to1991[i] <- mean(nodes2@data$bw1985to1991[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat2$bw1991to2001[i] <- mean(nodes2@data$bw1991to2001[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat2$bw2001to2010[i] <- mean(nodes2@data$bw2001to2010[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat2$bw2010to2020[i] <- mean(nodes2@data$bw2010to2020[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat2)
tail(tdat2)

#the historical anchor has to be picked separately for each city based on case knowledge
#for abuja, you're picking: 1977
historical_anchor <- 1977
tdat2$average_age <- ((historical_anchor + ((1985 - historical_anchor) / 2)) * tdat2$pre_1985 ) + 
((1985 + ((1991 - 1985) / 2)) * tdat2$bw1985to1991 ) + 
((1991 + ((2001 - 1991) / 2)) * tdat2$bw1991to2001 ) + 
((2001 + ((2010 - 2001) / 2)) * tdat2$bw2001to2010 ) + 
((2010 + ((2020 - 2010) / 2)) * tdat2$bw2010to2020 ) 
summary(tdat2$average_age)

dim(tdat)
dim(tdat2)

#### ADD HERE: DISTANCE TO THE CAPITAL AT THE TIME IT WAS THE CAPITAL: 
#plot(nodes)
	#tdat
load(paste("./boeing_data/tcommunities/", "abuja-2544", ".Rdata", sep="")	)

caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
head(caps)
caps <- caps[caps$city=="abuja-2544",]
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist[,1]  #if there is *MORE THAN 1* right here is where you'd assign them the average value for bands the overlap with the change over... this is going to be complicated to code... 
tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}

#tdat2 -- nodes 2 version... 

load(paste("./boeing_data/tcommunities/", "abuja-2565", ".Rdata", sep="")	)

caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
head(caps)
caps <- caps[caps$city=="abuja-2544",]
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
nodes2.aedc <- spTransform(nodes2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist <- gDistance(caps.pts1.aedc, nodes2.aedc, byid=T)
nodes2$dist_to_hq <- NA 
nodes2$dist_to_hq <- dist[,1]  #if there is *MORE THAN 1* right here is where you'd assign them the average value for bands the overlap with the change over... this is going to be complicated to code... 
tdat2$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat2)){
tdat2$avg_dist_to_hq[i] <- mean(nodes2@data$dist_to_hq[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}


table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


table(edges2$highway)
boulevards2 <- edges2[edges2$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['primary', 'primary_link']", "['primary', 'secondary']", "['primary', 'trunk_link']"),] #note: these will be somewhat different for each city
dim(boulevards2)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards2$b_id <- 1:nrow(boulevards2)


#pre_1985
test <- over(boulevards, t1985, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1985 <- 0
boulevards$pre_1985[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1985)

test <- over(boulevards2, t1985, returnList=F)
length(test)
dim(boulevards2)
head(test)
boulevards2$pre_1985 <- 0
boulevards2$pre_1985[is.na(test)==F] <- 1
table(test)
table(boulevards2$pre_1985)

#plot(t1985)
#plot(boulevards2[boulevards2$pre_1985==1,], col="green", cex=0.8, add=T)
#plot(boulevards[boulevards$pre_1985==1,], col="blue", cex=0.8, add=T)

#bw1985to1991
test <- over(boulevards, t1991, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1991 <- 0
boulevards$bw1985to1991[is.na(test$id)==F & boulevards$pre_1985 ==0] <- 1
table(boulevards$pre_1985, boulevards$bw1985to1991)

test <- over(boulevards2, t1991, returnList=F)
dim(test)
dim(boulevards2)
head(test)
boulevards2$bw1985to1991 <- 0
boulevards2$bw1985to1991[is.na(test$id)==F & boulevards2$pre_1985 ==0] <- 1
table(boulevards2$pre_1985, boulevards2$bw1985to1991)

#bw1991to2001
test <- over(boulevards, t2001, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1991to2001 <- 0
boulevards$bw1991to2001[is.na(test$id)==F & boulevards$pre_1985 ==0 & boulevards$bw1985to1991 ==0] <- 1
table(boulevards$bw1985to1991, boulevards$bw1991to2001)

test <- over(boulevards2, t2001, returnList=F)
dim(test)
dim(boulevards2)
head(test)
boulevards2$bw1991to2001 <- 0
boulevards2$bw1991to2001[is.na(test$id)==F & boulevards2$pre_1985 ==0 & boulevards2$bw1985to1991 ==0] <- 1
table(boulevards2$bw1985to1991, boulevards2$bw1991to2001)

#bw2001to2010
test <- over(boulevards, t2010, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2001to2010 <- 0
boulevards$bw2001to2010[is.na(test$id)==F & boulevards$pre_1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001 ==0] <- 1
table(boulevards$bw1991to2001, boulevards$bw2001to2010)

test <- over(boulevards2, t2010, returnList=F)
dim(test)
dim(boulevards2)
head(test)
boulevards2$bw2001to2010 <- 0
boulevards2$bw2001to2010[is.na(test$id)==F & boulevards2$pre_1985 ==0 & boulevards2$bw1985to1991 ==0 & boulevards2$bw1991to2001 ==0] <- 1
table(boulevards2$bw1991to2001, boulevards2$bw2001to2010)

#bw2010to2020
boulevards$bw2010to2020 <- 0
boulevards$bw2010to2020[ boulevards$pre_1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001 ==0 & boulevards$bw2001to2010 ==0] <- 1

boulevards2$bw2010to2020 <- 0
boulevards2$bw2010to2020[ boulevards2$pre_1985 ==0 & boulevards2$bw1985to1991 ==0 & boulevards2$bw1991to2001 ==0 & boulevards2$bw2001to2010 ==0] <- 1



## BOULEVARDS 1 VERSION


#load in the graphml object for this city (the original one)
load(paste("./boeing_data/tcommunities/", "abuja-2544", ".Rdata", sep="")	)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[1], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2a <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2a <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2a@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2a <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2a <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2a, boulevards2a, returnList=F)
nodes3 <- nodes2a[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2a$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km





## BOULEVARDS 2 VERSION

#load in the graphml object for this city (the original one)
load(paste("./boeing_data/tcommunities/", "abuja-2565", ".Rdata", sep="")	)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[2], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes2)[! names(nodes2) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes2$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2a <- nodes2[apply(nodes2@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2a <- nodes2[nodes2@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2a@data$osmid)
summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2a <- boulevards2[apply(boulevards2@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2a <- boulevards2[boulevards2@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2a, boulevards2a, returnList=F)
nodes3 <- nodes2a[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes2[nodes2@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2a$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes2$dist_to_boulevard[nodes2$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes2$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat2$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat2)){

tdat2$avg_dist_to_boulevard[i] <- mean(nodes2@data$dist_to_boulevard[nodes2@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat2$avg_dist_to_boulevard <- tdat2$avg_dist_to_boulevard / 1000 #put it in km



tdat <- rbind(tdat, tdat2)
dim(tdat)

save(tdat, file=paste("./boeing_data/tcommunities/", "abuja-combined", "_tdat_wyears_NEW", ".Rdata", sep="")	)

############################
##### CITY 2: ACCRA ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/ghana-GHA_gpkg/accra-1910.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/ghana-GHA_gpkg/accra-1910.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "accra-1910", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "accra-1910", ".Rdata", sep="")	)


#urban extent at 1920 (just use this one to add it to the 1927 one)
t1920 <- readOGR("./extents/accra/accra1920_13355000.shp")
class(t1920)

#plot(t1920)
t1920 <- spTransform(t1920, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#collapse down to a union of all the subshapes
#t1920a <- gUnaryUnion(t1920)
t1920a <- aggregate(t1920)
#plot(t1920a)
#plot(t1920, col="orange")
#plot(t1920a, col="green", add=T)
class(t1920a)
length(t1920a)

#urban extent at 1927
t1927 <- readOGR("./extents/accra/accra1927.shp")
class(t1927)
t1927 <- spTransform(t1927, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

t1927a <- aggregate(t1927)

#plot(t1927a)
#plot(t1920a, col="green", add=T)

dat <- as.data.frame(matrix(NA, nrow=1, ncol=1))
colnames(dat) <- "x"
dat$x <- 1
t1927a <- SpatialPolygonsDataFrame(t1927a, data=dat)

dat <- as.data.frame(matrix(NA, nrow=1, ncol=1))
colnames(dat) <- "x"
dat$x <- 1
t1920a <- SpatialPolygonsDataFrame(t1920a, data=dat)

union <- union(t1927a, t1920a)
#plot(union, col="red", add=T)

t1927full <- aggregate(union)  #this is the 1927 document! 


#urban extent at 1945:
t1945 <- readOGR("./extents/accra/accra1945.shp")
class(t1945)

#urban extent at 1957
t1957 <- readOGR("./extents/accra/accra1957.shp")
class(t1957)
t1957 <- spTransform(t1957, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 1966
t1966 <- readOGR("./extents/accra/accra1966.shp")
class(t1966)
t1966 <- spTransform(t1966, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 1985 -- google maps
t1985 <- readOGR("./extents/accra/accra1985_additionsonly.shp")
class(t1985)
t1985 <- spTransform(t1985, CRS(" +proj=longlat +datum=WGS84 +no_defs "))


#urban extent at 1991
t1991 <- readOGR("./extents/accra/urbFootprint_t1_subset_pruned.shp")
class(t1991)
#spatial polygons
	#these are in different coordinate reference systems, so can't overlay them
t1991 <- spTransform(t1991, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 2001 (note missing tema)
t2001 <- readOGR("./extents/accra/urbFootprint_t2_subset_pruned.shp")
class(t2001)
#spatial polygons
	#these are in different coordinate reference systems, so can't overlay them
t2001 <- spTransform(t2001, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 2008 -- google maps
t2008 <- readOGR("./extents/accra/accra2008_additionsonly.shp")
class(t2008)
t2008 <- spTransform(t2008, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 2014
t2014 <- readOGR("./extents/accra/urbFootprint_t3_subset_pruned.shp")
class(t2014)
#spatial polygons
	#these are in different coordinate reference systems, so can't overlay them
t2014 <- spTransform(t2014, CRS(" +proj=longlat +datum=WGS84 +no_defs "))


#pre_1927
test <- over(nodes, t1927full)
length(test)
dim(nodes)
head(test)
nodes$pre_1927 <- 0
nodes$pre_1927[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1927)
#plot(t2001full)
#plot(nodes[nodes$pre_1927==1,], col="blue", pch=16, cex=0.7, add=T)

#bw1927to1957
test <- over(nodes, t1945)
dim(test)
dim(nodes)
head(test)
nodes$bw1927to1945 <- 0
nodes$bw1927to1945[is.na(test$id)==F & nodes$pre_1927 ==0] <- 1
table(nodes$pre_1927, nodes$bw1927to1945)

#bw1945to1957
test <- over(nodes, t1957)
dim(test)
dim(nodes)
head(test)
nodes$bw1945to1957 <- 0
nodes$bw1945to1957[is.na(test$id)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0] <- 1
table(nodes$pre_1927, nodes$bw1945to1957)
table(nodes$bw1927to1945, nodes$bw1945to1957)

#bw1957to1966
test <- over(nodes, t1966)
length(test)
dim(nodes)
head(test)
nodes$bw1957to1966 <- 0
nodes$bw1957to1966[is.na(test)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0] <- 1
table(nodes$pre_1927, nodes$bw1957to1966)
table(nodes$bw1927to1945, nodes$bw1957to1966)
table(nodes$bw1945to1957, nodes$bw1957to1966)

#bw1966to1985
test <- over(nodes, t1985)
length(test)
dim(nodes)
head(test)
nodes$bw1966to1985 <- 0
nodes$bw1966to1985[is.na(test$id)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0 & nodes$bw1957to1966==0] <- 1
table(nodes$pre_1927, nodes$bw1966to1985)
table(nodes$bw1927to1945, nodes$bw1966to1985)
table(nodes$bw1945to1957, nodes$bw1966to1985)
table(nodes$bw1957to1966, nodes$bw1966to1985)

#bw1985to1991
test <- over(nodes, t1991)
dim(nodes)
head(test)
dim(test)
nodes$bw1985to1991 <- 0
nodes$bw1985to1991[is.na(test$fid)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1985==0] <- 1
table(nodes$pre_1927, nodes$bw1985to1991)
table(nodes$bw1927to1945, nodes$bw1985to1991)
table(nodes$bw1945to1957, nodes$bw1985to1991)
table(nodes$bw1957to1966, nodes$bw1985to1991)
table(nodes$bw1966to1985, nodes$bw1985to1991)

#bw1991to2001
test <- over(nodes, t2001)
dim(nodes)
head(test)
dim(test)
nodes$bw1991to2001 <- 0
nodes$bw1991to2001[is.na(test$fid)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1985==0 & nodes$bw1985to1991 ==0] <- 1
table(nodes$pre_1927, nodes$bw1991to2001)
table(nodes$bw1927to1945, nodes$bw1991to2001)
table(nodes$bw1945to1957, nodes$bw1991to2001)
table(nodes$bw1957to1966, nodes$bw1991to2001)
table(nodes$bw1966to1985, nodes$bw1991to2001)
table(nodes$bw1985to1991, nodes$bw1991to2001)

#plot(t2014full)

#bw2001to2008
test <- over(nodes, t2008)
dim(nodes)
head(test)
dim(test)
nodes$bw2001to2008 <- 0
nodes$bw2001to2008[is.na(test$id)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1985==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001==0] <- 1
table(nodes$pre_1927, nodes$bw2001to2008)
table(nodes$bw1927to1945, nodes$bw2001to2008)
table(nodes$bw1945to1957, nodes$bw2001to2008)
table(nodes$bw1957to1966, nodes$bw2001to2008)
table(nodes$bw1966to1985, nodes$bw2001to2008)
table(nodes$bw1985to1991, nodes$bw2001to2008)
table(nodes$bw1991to2001, nodes$bw2001to2008)


#bw2008to2014
test <- over(nodes, t2014)
dim(nodes)
head(test)
dim(test)
nodes$bw2008to2014 <- 0
nodes$bw2008to2014[is.na(test$fid)==F & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1985==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001==0 & nodes$bw2001to2008==0] <- 1

#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1927 ==0 & nodes$pre_1927 ==0 & nodes$bw1927to1945==0 & nodes$bw1945to1957==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1985==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001==0 & nodes$bw2001to2008==0 & nodes$bw2008to2014==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1927 <- NA
tdat$bw1927to1945 <- NA
tdat$bw1945to1957 <- NA
tdat$bw1957to1966 <- NA
tdat$bw1966to1985 <- NA
tdat$bw1985to1991 <- NA
tdat$bw1991to2001 <- NA
tdat$bw2001to2008 <- NA
tdat$bw2008to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1927[i] <- mean(nodes@data$pre_1927[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1927to1945[i] <- mean(nodes@data$bw1927to1945[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1945to1957[i] <- mean(nodes@data$bw1945to1957[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1957to1966[i] <- mean(nodes@data$bw1957to1966[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1966to1985[i] <- mean(nodes@data$bw1966to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1991[i] <- mean(nodes@data$bw1985to1991[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1991to2001[i] <- mean(nodes@data$bw1991to2001[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2001to2008[i] <- mean(nodes@data$bw2001to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2014[i] <- mean(nodes@data$bw2008to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
}

head(tdat)
tail(tdat)

historical_anchor <- 1877 #when accra becomes british capital
tdat$average_age <- ((historical_anchor + ((1927 - historical_anchor) / 2)) * tdat$pre_1927 ) + 
((1927 + ((1945 - 1927) / 2)) * tdat$bw1927to1945 ) + 
((1945 + ((1957 - 1945) / 2)) * tdat$bw1945to1957 ) + 
((1957 + ((1966 - 1957) / 2)) * tdat$bw1957to1966 ) + 
((1966 + ((1985 - 1966) / 2)) * tdat$bw1966to1985 ) + 
((1985 + ((1991 - 1985) / 2)) * tdat$bw1985to1991 ) + 
((1991 + ((2001 - 1991) / 2)) * tdat$bw1991to2001 ) + 
((2001 + ((2008 - 2001) / 2)) * tdat$bw2001to2008 ) + 
((2008 + ((2014 - 2008) / 2)) * tdat$bw2008to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="accra-1910",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$bw2014to2020!=1] <- dist1[nodes$bw2014to2020!=1,1] 
nodes$dist_to_hq[nodes$bw2014to2020==1] <- (dist1[nodes$bw2014to2020==1,1] * (3/7)) + (dist2[nodes$bw2014to2020==1,1] * (4/7)) #just the time period that is crossed the swithceroo
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#### add here distance to "boulevards", defined as primary /secondary roads and highways *THAT EXISTED AT THE TIME THE NEIGHBORHOOD EXISTED* 

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)

#date the highways:

#pre_1927
test <- over(boulevards, t1927full)
length(test) == nrow(boulevards)
head(test)
boulevards $pre_1927 <- 0
boulevards $pre_1927[is.na(test)==F] <- 1
table(boulevards $pre_1927)

#bw1927to1957
test <- over(boulevards, t1945)
dim(test)
head(test)
boulevards $bw1927to1945 <- 0
boulevards $bw1927to1945[is.na(test$id)==F & boulevards $pre_1927 ==0] <- 1
table(boulevards $pre_1927, boulevards $bw1927to1945)

#bw1945to1957
test <- over(boulevards, t1957)
dim(test)
dim(boulevards)
head(test)
boulevards $bw1945to1957 <- 0
boulevards $bw1945to1957[is.na(test$id)==F & boulevards $pre_1927 ==0 & boulevards $bw1927to1945==0] <- 1
table(boulevards $pre_1927, boulevards $bw1945to1957)
table(boulevards $bw1927to1945, boulevards $bw1945to1957)


#bw1957to1966
test <- over(boulevards, t1966)
length(test)
dim(boulevards)
head(test)
boulevards$bw1957to1966 <- 0
boulevards$bw1957to1966[is.na(test)==F & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0] <- 1
table(boulevards$pre_1927, boulevards$bw1957to1966)
table(boulevards$bw1927to1945, boulevards$bw1957to1966)
table(boulevards$bw1945to1957, boulevards$bw1957to1966)

#bw1966to1985
test <- over(boulevards, t1985)
length(test)
dim(boulevards)
head(test)
boulevards$bw1966to1985 <- 0
boulevards$bw1966to1985[is.na(test$id)==F & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0 & boulevards$bw1957to1966==0] <- 1
table(boulevards$pre_1927, boulevards$bw1966to1985)
table(boulevards$bw1927to1945, boulevards$bw1966to1985)
table(boulevards$bw1945to1957, boulevards$bw1966to1985)
table(boulevards$bw1957to1966, boulevards$bw1966to1985)

#bw1985to1991
test <- over(boulevards, t1991, returnList=F) #this is where the code gets slow; is it because the boulevards intersect multiple t1991 locations? i think that must be it?  #returnList=F means its only returnign the first mathc; not all the matches; i don't care about all the matches, just if it matches at all
dim(boulevards)
head(test)
dim(test)
boulevards$bw1985to1991 <- 0
boulevards$bw1985to1991[is.na(test$fid)==F & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1985==0] <- 1
table(boulevards$pre_1927, boulevards$bw1985to1991)
table(boulevards$bw1927to1945, boulevards$bw1985to1991)
table(boulevards$bw1945to1957, boulevards$bw1985to1991)
table(boulevards$bw1957to1966, boulevards$bw1985to1991)
table(boulevards$bw1966to1985, boulevards$bw1985to1991)

#bw1991to2001
test <- over(boulevards, t2001,  returnList=F)
dim(boulevards)
head(test)
dim(test)
boulevards$bw1991to2001 <- 0
boulevards$bw1991to2001[is.na(test$fid)==F & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1985==0 & boulevards$bw1985to1991 ==0] <- 1
table(boulevards$pre_1927, boulevards$bw1991to2001)
table(boulevards$bw1927to1945, boulevards$bw1991to2001)
table(boulevards$bw1945to1957, boulevards$bw1991to2001)
table(boulevards$bw1957to1966, boulevards$bw1991to2001)
table(boulevards$bw1966to1985, boulevards$bw1991to2001)
table(boulevards$bw1985to1991, boulevards$bw1991to2001)

#plot(t2014full)

#bw2001to2008
test <- over(boulevards, t2008)
dim(boulevards)
head(test)
dim(test)
boulevards$bw2001to2008 <- 0
boulevards$bw2001to2008[is.na(test$id)==F & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1985==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001==0] <- 1
table(boulevards$pre_1927, boulevards$bw2001to2008)
table(boulevards$bw1927to1945, boulevards$bw2001to2008)
table(boulevards$bw1945to1957, boulevards$bw2001to2008)
table(boulevards$bw1957to1966, boulevards$bw2001to2008)
table(boulevards$bw1966to1985, boulevards$bw2001to2008)
table(boulevards$bw1985to1991, boulevards$bw2001to2008)
table(boulevards$bw1991to2001, boulevards$bw2001to2008)


#bw2008to2014
test <- over(boulevards, t2014, returnList=F)
dim(boulevards)
head(test)
dim(test)
boulevards$bw2008to2014 <- 0
boulevards$bw2008to2014[is.na(test$fid)==F & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1985==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001==0 & boulevards$bw2001to2008==0] <- 1

#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1927 ==0 & boulevards$pre_1927 ==0 & boulevards$bw1927to1945==0 & boulevards$bw1945to1957==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1985==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001==0 & boulevards$bw2001to2008==0 & boulevards$bw2008to2014==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[3], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km
save(tdat, file=paste("./boeing_data/tcommunities/", "accra-1910", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 3: ADDIS ABABA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/ethiopia-ETH_gpkg/addis_ababa-5134.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/ethiopia-ETH_gpkg/addis_ababa-5134.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "addis_ababa-5134", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "addis_ababa-5134", ".Rdata", sep="")	)

#urban extent at 1959
t1959 <- readOGR("./extents/addis_ababa/addis1959.shp")
class(t1959)
proj4string(t1959)

#urban extent at 1964
t1964 <- readOGR("./extents/addis_ababa/addis1964.shp")
class(t1964)
proj4string(t1964)

#urban extent at 1973
t1973 <- readOGR("./extents/addis_ababa/addis1973.shp")
class(t1973)
proj4string(t1973)

#urban extent at 1986
t1986 <- readOGR("./extents/addis_ababa/urbFootprint_t1_subset_pruned.shp")
class(t1986)
proj4string(t1986)

#urban extent at 1992
t1992 <- readOGR("./extents/addis_ababa/addis1992.shp")
class(t1992)
proj4string(t1992)

#urban extent at 2000
t2000 <- readOGR("./extents/addis_ababa/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2010
t2010 <- readOGR("./extents/addis_ababa/urbFootprint_t3_subset_pruned.shp")
class(t2010)
proj4string(t2010)


#pre_1959
test <- over(nodes, t1959)
length(test)
dim(nodes)
head(test)
nodes$pre_1959 <- 0
nodes$pre_1959[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1959)

#bw1959to1964
test <- over(nodes, t1964)
dim(test)
dim(nodes)
head(test)
nodes$bw1959to1964 <- 0
nodes$bw1959to1964[is.na(test$id)==F & nodes$pre_1959 ==0] <- 1
table(nodes$pre_1959, nodes$bw1959to1964)

#bw1964to1973
test <- over(nodes, t1973)
dim(test)
dim(nodes)
head(test)
nodes$bw1964to1973 <- 0
nodes$bw1964to1973[is.na(test$id)==F & nodes$pre_1959 ==0 & nodes$bw1959to1964 ==0] <- 1
table(nodes$bw1959to1964, nodes$bw1964to1973)

#bw1973to1986  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t1986)
dim(test)
dim(nodes)
head(test)
nodes$bw1973to1986 <- 0
nodes$bw1973to1986[is.na(test$fid)==F & nodes$pre_1959 ==0 & nodes$bw1959to1964 ==0 & nodes$bw1964to1973 == 0] <- 1
table(nodes$bw1964to1973, nodes$bw1973to1986)

#bw1986to1992
test <- over(nodes, t1992)
dim(test)
dim(nodes)
head(test)
nodes$bw1986to1992 <- 0
nodes$bw1986to1992[is.na(test$id)==F & nodes$pre_1959 ==0 & nodes$bw1959to1964 ==0 & nodes$bw1964to1973 == 0 & nodes$bw1973to1986==0] <- 1
table(nodes$bw1973to1986, nodes$bw1986to1992)


#bw1992to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1992to2000 <- 0
nodes$bw1992to2000[is.na(test$fid)==F & nodes$pre_1959 ==0 & nodes$bw1959to1964 ==0 & nodes$bw1964to1973 == 0 & nodes$bw1973to1986==0 & nodes$bw1986to1992 ==0] <- 1
table(nodes$bw1986to1992, nodes$bw1992to2000)

#bw2000to2010
test <- over(nodes, t2010)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2010 <- 0
nodes$bw2000to2010[is.na(test$fid)==F & nodes$pre_1959 ==0 & nodes$bw1959to1964 ==0 & nodes$bw1964to1973 == 0 & nodes$bw1973to1986==0 & nodes$bw1986to1992 ==0 & nodes$bw1992to2000==0] <- 1
table(nodes$bw1992to2000, nodes$bw2000to2010)

#bw2010to2020
nodes$bw2010to2020 <- 0
nodes$bw2010to2020[ nodes$pre_1959 ==0 & nodes$bw1959to1964 ==0 & nodes$bw1964to1973 == 0 & nodes$bw1973to1986==0 & nodes$bw1986to1992 ==0 & nodes$bw1992to2000==0 & nodes$bw2000to2010==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1959 <- NA
tdat$bw1959to1964 <- NA
tdat$bw1964to1973 <- NA
tdat$bw1973to1986 <- NA
tdat$bw1986to1992 <- NA
tdat$bw1992to2000 <- NA
tdat$bw2000to2010 <- NA
tdat$bw2010to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1959[i] <- mean(nodes@data$pre_1959[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1959to1964[i] <- mean(nodes@data$bw1959to1964[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1964to1973[i] <- mean(nodes@data$bw1964to1973[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1973to1986[i] <- mean(nodes@data$bw1973to1986[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1986to1992[i] <- mean(nodes@data$bw1986to1992[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1992to2000[i] <- mean(nodes@data$bw1992to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2010[i] <- mean(nodes@data$bw2000to2010[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2010to2020[i] <- mean(nodes@data$bw2010to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1886 #see: https://en.wikipedia.org/wiki/Addis_Ababa#History
tdat$average_age <- ((historical_anchor + ((1959 - historical_anchor) / 2)) * tdat$pre_1959 ) + 
((1959 + ((1964 - 1959) / 2)) * tdat$bw1959to1964 ) + 
((1964 + ((1973 - 1964) / 2)) * tdat$bw1964to1973 ) + 
((1973 + ((1986 - 1973) / 2)) * tdat$bw1973to1986 ) + 
((1986 + ((1992 - 1986) / 2)) * tdat$bw1986to1992 ) + 
((1992 + ((2000 - 1992) / 2)) * tdat$bw1992to2000 ) + 
((2000 + ((2010 - 2000) / 2)) * tdat$bw2000to2010 ) + 
((2010 + ((2020 - 2010) / 2)) * tdat$bw2010to2020 ) 
summary(tdat$average_age)


#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="addis_ababa-5134",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1959==1] <- dist1[nodes$pre_1959==1,1] 
nodes$dist_to_hq[nodes$pre_1959!=1] <- dist2[nodes$pre_1959!=1,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}


#### add here distance to "boulevards", defined as primary /secondary roads and highways *THAT EXISTED AT THE TIME THE NEIGHBORHOOD EXISTED* 

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#date the highways:


#pre_1959
test <- over(boulevards, t1959,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1959 <- 0
boulevards$pre_1959[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1959)

#bw1959to1964
test <- over(boulevards, t1964,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1959to1964 <- 0
boulevards$bw1959to1964[is.na(test$id)==F & boulevards$pre_1959 ==0] <- 1
table(boulevards$pre_1959, boulevards$bw1959to1964)

#bw1964to1973
test <- over(boulevards, t1973,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1964to1973 <- 0
boulevards$bw1964to1973[is.na(test$id)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1964 ==0] <- 1
table(boulevards$bw1959to1964, boulevards$bw1964to1973)

#bw1973to1986  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t1986,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1973to1986 <- 0
boulevards$bw1973to1986[is.na(test$fid)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1964 ==0 & boulevards$bw1964to1973 == 0] <- 1
table(boulevards$bw1964to1973, boulevards$bw1973to1986)

#bw1986to1992
test <- over(boulevards, t1992,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1986to1992 <- 0
boulevards$bw1986to1992[is.na(test$id)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1964 ==0 & boulevards$bw1964to1973 == 0 & boulevards$bw1973to1986==0] <- 1
table(boulevards$bw1973to1986, boulevards$bw1986to1992)

#bw1992to2000
test <- over(boulevards, t2000,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1992to2000 <- 0
boulevards$bw1992to2000[is.na(test$fid)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1964 ==0 & boulevards$bw1964to1973 == 0 & boulevards$bw1973to1986==0 & boulevards$bw1986to1992 ==0] <- 1
table(boulevards$bw1986to1992, boulevards$bw1992to2000)

#bw2000to2010
test <- over(boulevards, t2010,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2010 <- 0
boulevards$bw2000to2010[is.na(test$fid)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1964 ==0 & boulevards$bw1964to1973 == 0 & boulevards$bw1973to1986==0 & boulevards$bw1986to1992 ==0 & boulevards$bw1992to2000==0] <- 1
table(boulevards$bw1992to2000, boulevards$bw2000to2010)

#bw2010to2020
boulevards$bw2010to2020 <- 0
boulevards$bw2010to2020[ boulevards$pre_1959 ==0 & boulevards$bw1959to1964 ==0 & boulevards$bw1964to1973 == 0 & boulevards$bw1973to1986==0 & boulevards$bw1986to1992 ==0 & boulevards$bw1992to2000==0 & boulevards$bw2000to2010==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[4], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
#summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "addis_ababa-5134", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 4: ARUSHA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/tanzania-TZA_gpkg/arusha-4800.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/tanzania-TZA_gpkg/arusha-4800.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "arusha-4800", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "arusha-4800", ".Rdata", sep="")	)


#urban extent at 1955
t1955 <- readOGR("./extents/arusha/arusha1955.shp")
class(t1955)
proj4string(t1955)

#urban extent at 1967
t1967 <- readOGR("./extents/arusha/Arusha 1967 LOC 1.shp")
class(t1967)
proj4string(t1967)
t1967@data <- as.data.frame(matrix(1, nrow=nrow(t1967), ncol=1))
colnames(t1967@data) <- c("id")

#urban extent at 1988
t1988 <- readOGR("./extents/arusha/urbFootprint_t1_subset_pruned.shp")
class(t1988)
proj4string(t1988)

#urban extent at 2000
t2000 <- readOGR("./extents/arusha/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2005
t2005 <- readOGR("./extents/arusha/arusha2005.shp")
class(t2005)
proj4string(t2005)

#urban extent at 2013
t2013 <- readOGR("./extents/arusha/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)

#pre_1955
test <- over(nodes, t1955)
length(test)
dim(nodes)
head(test)
nodes$pre_1955 <- 0
nodes$pre_1955[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1955)

#bw1955to1967
test <- over(nodes, t1967)
dim(test)
dim(nodes)
head(test)
nodes$bw1955to1967 <- 0
nodes$bw1955to1967[is.na(test$id)==F & nodes$pre_1955 ==0] <- 1
table(nodes$pre_1955, nodes$bw1955to1967)

#bw1967to1988
test <- over(nodes, t1988)
dim(test)
dim(nodes)
head(test)
nodes$bw1967to1988 <- 0
nodes$bw1967to1988[is.na(test$fid)==F & nodes$pre_1955 ==0 & nodes$bw1955to1967 ==0] <- 1
table(nodes$bw1955to1967, nodes$bw1967to1988)

#bw1988to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1988to2000 <- 0
nodes$bw1988to2000[is.na(test$fid)==F & nodes$pre_1955 ==0 & nodes$bw1955to1967 ==0 & nodes$bw1967to1988 ==0] <- 1
table(nodes$bw1967to1988, nodes$bw1988to2000)

#bw2000to2005
test <- over(nodes, t2005)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2005 <- 0
nodes$bw2000to2005[is.na(test$id)==F & nodes$pre_1955 ==0 & nodes$bw1955to1967 ==0 & nodes$bw1967to1988 ==0 & nodes$bw1988to2000 ==0] <- 1
table(nodes$bw1988to2000, nodes$bw2000to2005)

#bw2005to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2005to2013 <- 0
nodes$bw2005to2013[is.na(test$fid)==F & nodes$pre_1955 ==0 & nodes$bw1955to1967 ==0 & nodes$bw1967to1988 ==0 & nodes$bw1988to2000 ==0 & nodes$bw2000to2005 ==0] <- 1
table(nodes$bw2000to2005, nodes$bw2005to2013)


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1955 ==0 & nodes$bw1955to1967 ==0 & nodes$bw1967to1988 ==0 & nodes$bw1988to2000 ==0 & nodes$bw2000to2005 ==0 & nodes$bw2005to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1955 <- NA
tdat$bw1955to1967 <- NA
tdat$bw1967to1988 <- NA
tdat$bw1988to2000 <- NA
tdat$bw2000to2005 <- NA
tdat$bw2005to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1955[i] <- mean(nodes@data$pre_1955[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1955to1967[i] <- mean(nodes@data$bw1955to1967[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1967to1988[i] <- mean(nodes@data$bw1967to1988[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1988to2000[i] <- mean(nodes@data$bw1988to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2005[i] <- mean(nodes@data$bw2000to2005[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2005to2013[i] <- mean(nodes@data$bw2005to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1835 #first settled "in the 1830s"
tdat$average_age <- ((historical_anchor + ((1955 - historical_anchor) / 2)) * tdat$pre_1955 ) + 
((1955 + ((1967 - 1955) / 2)) * tdat$bw1955to1967 ) + 
((1967 + ((1988 - 1967) / 2)) * tdat$bw1967to1988 ) + 
((1988 + ((2000 - 1988) / 2)) * tdat$bw1988to2000 ) + 
((2000 + ((2005 - 2000) / 2)) * tdat$bw2000to2005 ) + 
((2005 + ((2013 - 2005) / 2)) * tdat$bw2005to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="arusha-4800",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1]
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}


#### add here distance to "boulevards", defined as primary /secondary roads and highways *THAT EXISTED AT THE TIME THE NEIGHBORHOOD EXISTED* 

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#date the highways:


#pre_1955
test <- over(boulevards, t1955,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1955 <- 0
boulevards$pre_1955[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1955)

#bw1955to1967
test <- over(boulevards, t1967,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1955to1967 <- 0
boulevards$bw1955to1967[is.na(test$id)==F & boulevards$pre_1955 ==0] <- 1
table(boulevards$pre_1955, boulevards$bw1955to1967)

#bw1967to1988
test <- over(boulevards, t1988,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1967to1988 <- 0
boulevards$bw1967to1988[is.na(test$fid)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1967 ==0] <- 1
table(boulevards$bw1955to1967, boulevards$bw1967to1988)

#bw1988to2000
test <- over(boulevards, t2000,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1988to2000 <- 0
boulevards$bw1988to2000[is.na(test$fid)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1967 ==0 & boulevards$bw1967to1988 ==0] <- 1
table(boulevards$bw1967to1988, boulevards$bw1988to2000)

#bw2000to2005
test <- over(boulevards, t2005,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2005 <- 0
boulevards$bw2000to2005[is.na(test$id)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1967 ==0 & boulevards$bw1967to1988 ==0 & boulevards$bw1988to2000 ==0] <- 1
table(boulevards$bw1988to2000, boulevards$bw2000to2005)

#bw2005to2013
test <- over(boulevards, t2013,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2005to2013 <- 0
boulevards$bw2005to2013[is.na(test$fid)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1967 ==0 & boulevards$bw1967to1988 ==0 & boulevards$bw1988to2000 ==0 & boulevards$bw2000to2005 ==0] <- 1
table(boulevards$bw2000to2005, boulevards$bw2005to2013)


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1955 ==0 & boulevards$bw1955to1967 ==0 & boulevards$bw1967to1988 ==0 & boulevards$bw1988to2000 ==0 & boulevards$bw2000to2005 ==0 & boulevards$bw2005to2013 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[5], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



head(tdat)
save(tdat, file=paste("./boeing_data/tcommunities/", "arusha-4800", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 5: BAMAKO ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/mali-MLI_gpkg/bamako-1553.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/mali-MLI_gpkg/bamako-1553.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "bamako-1553", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "bamako-1553", ".Rdata", sep="")	)



#urban extent at 1966
t1966 <- readOGR("./extents/bamako/bamako1966.shp")
class(t1966)
proj4string(t1966)

#urban extent at 1980
t1980 <- readOGR("./extents/bamako/bamako1980.shp")
class(t1980)
proj4string(t1980)

#urban extent at 1990 -- LINCOLN
t1990 <- readOGR("./extents/bamako/urbFootprint_t1_subset_pruned.shp")
class(t1990)
proj4string(t1990)

#urban extent at 2000 -- LINCOLN
t2000 <- readOGR("./extents/bamako/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2008
t2008 <- readOGR("./extents/bamako/bamako2008.shp")
class(t2008)
proj4string(t2008)

#urban extent at 2013 -- LINCOLN
t2013 <- readOGR("./extents/bamako/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)


#pre_1966
test <- over(nodes, t1966)
length(test)
dim(nodes)
head(test)
nodes$pre_1966 <- 0
nodes$pre_1966[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1966)

#bw1966to1980
test <- over(nodes, t1980)
dim(test)
dim(nodes)
head(test)
nodes$bw1966to1980 <- 0
nodes$bw1966to1980[is.na(test$id)==F & nodes$pre_1966 ==0] <- 1
table(nodes$pre_1966, nodes$bw1966to1980)

#bw1980to1990  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1980to1990 <- 0
nodes$bw1980to1990[is.na(test$fid)==F & nodes$pre_1966 ==0 & nodes$bw1966to1980 ==0 ] <- 1
table(nodes$bw1966to1980, nodes$bw1980to1990)

#bw1990to2000  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to2000 <- 0
nodes$bw1990to2000[is.na(test$fid)==F & nodes$pre_1966 ==0 & nodes$bw1966to1980 ==0 & nodes$bw1980to1990 ==0 ] <- 1
table(nodes$bw1980to1990, nodes$bw1990to2000)

#bw2000to2008
test <- over(nodes, t2008)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2008 <- 0
nodes$bw2000to2008[is.na(test$id)==F & nodes$pre_1966 ==0 & nodes$bw1966to1980 ==0 & nodes$bw1980to1990 ==0 & nodes$bw1990to2000 == 0] <- 1
table(nodes$bw1990to2000, nodes$bw2000to2008)

#bw2008to2013  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2008to2013 <- 0
nodes$bw2008to2013[is.na(test$fid)==F  & nodes$pre_1966 ==0 & nodes$bw1966to1980 ==0 & nodes$bw1980to1990 ==0 & nodes$bw1990to2000 == 0 & nodes$bw2000to2008 ==0] <- 1
table(nodes$bw2000to2008, nodes$bw2008to2013)

#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1966 ==0 & nodes$bw1966to1980 ==0 & nodes$bw1980to1990 ==0 & nodes$bw1990to2000 == 0 & nodes$bw2000to2008 ==0 & nodes$bw2008to2013 ==0 ] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1966 <- NA
tdat$bw1966to1980 <- NA
tdat$bw1980to1990 <- NA
tdat$bw1990to2000 <- NA
tdat$bw2000to2008 <- NA
tdat$bw2008to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1966[i] <- mean(nodes@data$pre_1966[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1966to1980[i] <- mean(nodes@data$bw1966to1980[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1980to1990[i] <- mean(nodes@data$bw1980to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to2000[i] <- mean(nodes@data$bw1990to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2008[i] <- mean(nodes@data$bw2000to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2013[i] <- mean(nodes@data$bw2008to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1883 #date french build a fort (likely date of formal street grid from that point) #settlement ot the 14th century at least. 
tdat$average_age <- ((historical_anchor + ((1966 - historical_anchor) / 2)) * tdat$pre_1966 ) + 
((1966 + ((1980 - 1966) / 2)) * tdat$bw1966to1980 ) + 
((1980 + ((1990 - 1980) / 2)) * tdat$bw1980to1990 ) + 
((1990 + ((2000 - 1990) / 2)) * tdat$bw1990to2000 ) + 
((2000 + ((2008 - 2000) / 2)) * tdat$bw2000to2008 ) + 
((2008 + ((2013 - 2008) / 2)) * tdat$bw2008to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)


#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="bamako-1553",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist2[,1] 
#only variation is before 1908, before this matters for any analysis you're going to do
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}


#### add here distance to "boulevards", defined as primary /secondary roads and highways *THAT EXISTED AT THE TIME THE NEIGHBORHOOD EXISTED* 

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1966
test <- over(boulevards, t1966,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1966 <- 0
boulevards$pre_1966[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1966)

#bw1966to1980
test <- over(boulevards, t1980,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1966to1980 <- 0
boulevards$bw1966to1980[is.na(test$id)==F & boulevards$pre_1966 ==0] <- 1
table(boulevards$pre_1966, boulevards$bw1966to1980)

#bw1980to1990  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1980to1990 <- 0
boulevards$bw1980to1990[is.na(test$fid)==F & boulevards$pre_1966 ==0 & boulevards$bw1966to1980 ==0 ] <- 1
table(boulevards$bw1966to1980, boulevards$bw1980to1990)

#bw1990to2000  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to2000 <- 0
boulevards$bw1990to2000[is.na(test$fid)==F & boulevards$pre_1966 ==0 & boulevards$bw1966to1980 ==0 & boulevards$bw1980to1990 ==0 ] <- 1
table(boulevards$bw1980to1990, boulevards$bw1990to2000)

#bw2000to2008
test <- over(boulevards, t2008,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2008 <- 0
boulevards$bw2000to2008[is.na(test$id)==F & boulevards$pre_1966 ==0 & boulevards$bw1966to1980 ==0 & boulevards$bw1980to1990 ==0 & boulevards$bw1990to2000 == 0] <- 1
table(boulevards$bw1990to2000, boulevards$bw2000to2008)

#bw2008to2013  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2013, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2008to2013 <- 0
boulevards$bw2008to2013[is.na(test$fid)==F  & boulevards$pre_1966 ==0 & boulevards$bw1966to1980 ==0 & boulevards$bw1980to1990 ==0 & boulevards$bw1990to2000 == 0 & boulevards$bw2000to2008 ==0] <- 1
table(boulevards$bw2000to2008, boulevards$bw2008to2013)

#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1966 ==0 & boulevards$bw1966to1980 ==0 & boulevards$bw1980to1990 ==0 & boulevards$bw1990to2000 == 0 & boulevards$bw2000to2008 ==0 & boulevards$bw2008to2013 ==0 ] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[6], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


head(tdat)
save(tdat, file=paste("./boeing_data/tcommunities/", "bamako-1553", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 6: BEIRA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/mozambique-MOZ_gpkg/beira-4521.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/mozambique-MOZ_gpkg/beira-4521.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "beira-4521", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "beira-4521", ".Rdata", sep="")	)

#urban extent at 1969
t1969 <- readOGR("./extents/beira/beira1969.shp")
class(t1969)
proj4string(t1969)

#urban extent at 1985
t1985 <- readOGR("./extents/beira/beira1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1991
t1991 <- readOGR("./extents/beira/urbFootprint_t1_subset_pruned.shp")
class(t1991)
proj4string(t1991)

#urban extent at 2001
t2001 <- readOGR("./extents/beira/urbFootprint_t2_subset_pruned.shp")
class(t2001)
proj4string(t2001)

#urban extent at 2007
t2007 <- readOGR("./extents/beira/beira2007.shp")
class(t2007)
proj4string(t2007)

#urban extent at 2013
t2013 <- readOGR("./extents/beira/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)

#pre_1969
test <- over(nodes, t1969)
length(test)
dim(nodes)
head(test)
nodes$pre_1969 <- 0
nodes$pre_1969[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1969)

#bw1969to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1969to1985 <- 0
nodes$bw1969to1985[is.na(test$id)==F & nodes$pre_1969 ==0] <- 1
table(nodes$pre_1969, nodes$bw1969to1985)

#bw1985to1991  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t1991)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1991 <- 0
nodes$bw1985to1991[is.na(test$fid)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985 ==0 ] <- 1
table(nodes$bw1969to1985, nodes$bw1985to1991)

#bw1991to2001  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2001)
dim(test)
dim(nodes)
head(test)
nodes$bw1991to2001 <- 0
nodes$bw1991to2001[is.na(test$fid)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1991 ==0 ] <- 1
table(nodes$bw1985to1991, nodes$bw1991to2001)

#bw2001to2007
test <- over(nodes, t2007)
dim(test)
dim(nodes)
head(test)
nodes$bw2001to2007 <- 0
nodes$bw2001to2007[is.na(test$id)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001 ==0 ] <- 1
table(nodes$bw1991to2001, nodes$bw2001to2007)

#bw2007to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2007to2013 <- 0
nodes$bw2007to2013[is.na(test$fid)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001 ==0 & nodes$bw2001to2007 ==0 ] <- 1
table(nodes$bw2001to2007, nodes$bw2007to2013)

#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2001 ==0 & nodes$bw2001to2007 ==0 & nodes$bw2007to2013 ==0 ] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1969 <- NA
tdat$bw1969to1985 <- NA
tdat$bw1985to1991 <- NA
tdat$bw1991to2001 <- NA
tdat$bw2001to2007 <- NA
tdat$bw2007to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1969[i] <- mean(nodes@data$pre_1969[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1969to1985[i] <- mean(nodes@data$bw1969to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1991[i] <- mean(nodes@data$bw1985to1991[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1991to2001[i] <- mean(nodes@data$bw1991to2001[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2001to2007[i] <- mean(nodes@data$bw2001to2007[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2007to2013[i] <- mean(nodes@data$bw2007to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1890 #date portuguese found the city
tdat$average_age <- ((historical_anchor + ((1969 - historical_anchor) / 2)) * tdat$pre_1969 ) + 
((1969 + ((1985 - 1969) / 2)) * tdat$bw1969to1985 ) + 
((1985 + ((1991 - 1985) / 2)) * tdat$bw1985to1991 ) + 
((1991 + ((2001 - 1991) / 2)) * tdat$bw1991to2001 ) + 
((2001 + ((2007 - 2001) / 2)) * tdat$bw2001to2007 ) + 
((2007 + ((2013 - 2007) / 2)) * tdat$bw2007to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="beira-4521",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)





#pre_1969
test <- over(boulevards, t1969,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1969 <- 0
boulevards$pre_1969[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1969)

#bw1969to1985
test <- over(boulevards, t1985,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1969to1985 <- 0
boulevards$bw1969to1985[is.na(test$id)==F & boulevards$pre_1969 ==0] <- 1
table(boulevards$pre_1969, boulevards$bw1969to1985)

#bw1985to1991  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t1991, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1991 <- 0
boulevards$bw1985to1991[is.na(test$fid)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985 ==0 ] <- 1
table(boulevards$bw1969to1985, boulevards$bw1985to1991)

#bw1991to2001  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2001, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1991to2001 <- 0
boulevards$bw1991to2001[is.na(test$fid)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1991 ==0 ] <- 1
table(boulevards$bw1985to1991, boulevards$bw1991to2001)

#bw2001to2007
test <- over(boulevards, t2007,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2001to2007 <- 0
boulevards$bw2001to2007[is.na(test$id)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001 ==0 ] <- 1
table(boulevards$bw1991to2001, boulevards$bw2001to2007)

#bw2007to2013
test <- over(boulevards, t2013, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2007to2013 <- 0
boulevards$bw2007to2013[is.na(test$fid)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001 ==0 & boulevards$bw2001to2007 ==0 ] <- 1
table(boulevards$bw2001to2007, boulevards$bw2007to2013)

#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2001 ==0 & boulevards$bw2001to2007 ==0 & boulevards$bw2007to2013 ==0 ] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[7], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


head(tdat)
save(tdat, file=paste("./boeing_data/tcommunities/", "beira-4521", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 7: BRIKAMA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/gambia-GMB_gpkg/brikama-1458.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/gambia-GMB_gpkg/brikama-1458.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "brikama-1458", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "brikama-1458", ".Rdata", sep="")	)



#urban extent at 1971
t1971 <- readOGR("./extents/brikama/brikama1971.shp")
class(t1971)
proj4string(t1971)

#urban extent at 1985
t1985 <- readOGR("./extents/brikama/brikama1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 2004
t2004 <- readOGR("./extents/brikama/brikama2004.shp")
class(t2004)
proj4string(t2004)

#urban extent at 2010
t2010 <- readOGR("./extents/brikama/brikama2010.shp")
class(t2010)
proj4string(t2010)

#urban extent at 2017
t2017 <- readOGR("./extents/brikama/brikama2017.shp")
class(t2017)
proj4string(t2017)

#pre_1971
test <- over(nodes, t1971)
length(test)
dim(nodes)
head(test)
nodes$pre_1971 <- 0
nodes$pre_1971[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1971)

#bw1971to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1971to1985 <- 0
nodes$bw1971to1985[is.na(test$id)==F & nodes$pre_1971 ==0] <- 1
table(nodes$pre_1971, nodes$bw1971to1985)

#bw1985to2004
test <- over(nodes, t2004)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to2004 <- 0
nodes$bw1985to2004[is.na(test$id)==F & nodes$pre_1971 ==0 & nodes$bw1971to1985==0] <- 1
table(nodes$bw1971to1985, nodes$bw1985to2004)

#bw2004to2010
test <- over(nodes, t2010)
dim(test)
dim(nodes)
head(test)
nodes$bw2004to2010 <- 0
nodes$bw2004to2010[is.na(test$id)==F & nodes$pre_1971 ==0 & nodes$bw1971to1985==0 & nodes$bw1985to2004==0] <- 1
table(nodes$bw1985to2004, nodes$bw2004to2010)

#bw2010to2017
test <- over(nodes, t2017)
dim(test)
dim(nodes)
head(test)
nodes$bw2010to2017 <- 0
nodes$bw2010to2017[is.na(test$id)==F & nodes$pre_1971 ==0 & nodes$bw1971to1985==0 & nodes$bw1985to2004==0 & nodes$bw2004to2010==0] <- 1
table(nodes$bw2004to2010, nodes$bw2010to2017)

#bw2017to2020
nodes$bw2017to2020 <- 0
nodes$bw2017to2020[ nodes$pre_1971 ==0 & nodes$bw1971to1985==0 & nodes$bw1985to2004==0 & nodes$bw2004to2010==0 & nodes$bw2010to2017 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1971 <- NA
tdat$bw1971to1985 <- NA
tdat$bw1985to2004 <- NA
tdat$bw2004to2010 <- NA
tdat$bw2010to2017 <- NA
tdat$bw2017to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1971[i] <- mean(nodes@data$pre_1971[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1971to1985[i] <- mean(nodes@data$bw1971to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to2004[i] <- mean(nodes@data$bw1985to2004[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2004to2010[i] <- mean(nodes@data$bw2004to2010[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2010to2017[i] <- mean(nodes@data$bw2010to2017[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2017to2020[i] <- mean(nodes@data$bw2017to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1894 #date british rule extends to cover brikama area from banjul
tdat$average_age <- ((historical_anchor + ((1971 - historical_anchor) / 2)) * tdat$pre_1971 ) + 
((1971 + ((1985 - 1971) / 2)) * tdat$bw1971to1985 ) + 
((1985 + ((2004 - 1985) / 2)) * tdat$bw1985to2004 ) + 
((2004 + ((2010 - 2004) / 2)) * tdat$bw2004to2010 ) + 
((2010 + ((2017 - 2010) / 2)) * tdat$bw2010to2017 ) + 
((2017 + ((2020 - 2017) / 2)) * tdat$bw2017to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="brikama-1458",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1971
test <- over(boulevards, t1971,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1971 <- 0
boulevards$pre_1971[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1971)

#bw1971to1985
test <- over(boulevards, t1985,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1971to1985 <- 0
boulevards$bw1971to1985[is.na(test$id)==F & boulevards$pre_1971 ==0] <- 1
table(boulevards$pre_1971, boulevards$bw1971to1985)

#bw1985to2004
test <- over(boulevards, t2004,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to2004 <- 0
boulevards$bw1985to2004[is.na(test$id)==F & boulevards$pre_1971 ==0 & boulevards$bw1971to1985==0] <- 1
table(boulevards$bw1971to1985, boulevards$bw1985to2004)

#bw2004to2010
test <- over(boulevards, t2010,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2004to2010 <- 0
boulevards$bw2004to2010[is.na(test$id)==F & boulevards$pre_1971 ==0 & boulevards$bw1971to1985==0 & boulevards$bw1985to2004==0] <- 1
table(boulevards$bw1985to2004, boulevards$bw2004to2010)

#bw2010to2017
test <- over(boulevards, t2017,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2010to2017 <- 0
boulevards$bw2010to2017[is.na(test$id)==F & boulevards$pre_1971 ==0 & boulevards$bw1971to1985==0 & boulevards$bw1985to2004==0 & boulevards$bw2004to2010==0] <- 1
table(boulevards$bw2004to2010, boulevards$bw2010to2017)

#bw2017to2020
boulevards$bw2017to2020 <- 0
boulevards$bw2017to2020[ boulevards$pre_1971 ==0 & boulevards$bw1971to1985==0 & boulevards$bw1985to2004==0 & boulevards$bw2004to2010==0 & boulevards$bw2010to2017 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[8], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "brikama-1458", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 8: BUCHANAN ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/liberia-LBR_gpkg/buchanan-1533.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/liberia-LBR_gpkg/buchanan-1533.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "buchanan-1533", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "buchanan-1533", ".Rdata", sep="")	)


#urban extent at 1969
t1969 <- readOGR("./extents/buchanan/buchanan1969.shp")
class(t1969)
proj4string(t1969)

#urban extent at 1985
t1985 <- readOGR("./extents/buchanan/buchanan1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1995
t1995 <- readOGR("./extents/buchanan/buchanan1995.shp")
class(t1995)
proj4string(t1995)

#urban extent at 2003
t2003 <- readOGR("./extents/buchanan/buchanan2003.shp")
class(t2003)
proj4string(t2003)

#urban extent at 2013
t2013 <- readOGR("./extents/buchanan/buchanan2013.shp")
class(t2013)
proj4string(t2013)

#pre_1969
test <- over(nodes, t1969)
length(test)
dim(nodes)
head(test)
nodes$pre_1969 <- 0
nodes$pre_1969[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1969)

#bw1969to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1969to1985 <- 0
nodes$bw1969to1985[is.na(test$id)==F & nodes$pre_1969 ==0] <- 1
table(nodes$pre_1969, nodes$bw1969to1985)

#bw1985to1995
test <- over(nodes, t1995)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1995 <- 0
nodes$bw1985to1995[is.na(test$id)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985==0] <- 1
table(nodes$bw1969to1985, nodes$bw1985to1995)

#bw1995to2003
test <- over(nodes, t2003)
dim(test)
dim(nodes)
head(test)
nodes$bw1995to2003 <- 0
nodes$bw1995to2003[is.na(test$id)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985==0 & nodes$bw1985to1995 ==0] <- 1
table(nodes$bw1985to1995, nodes$bw1995to2003)

#bw2003to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2003to2013 <- 0
nodes$bw2003to2013[is.na(test$id)==F & nodes$pre_1969 ==0 & nodes$bw1969to1985==0 & nodes$bw1985to1995 ==0 & nodes$bw1995to2003 ==0] <- 1
table(nodes$bw1995to2003, nodes$bw2003to2013)

#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1969 ==0 & nodes$bw1969to1985==0 & nodes$bw1985to1995 ==0 & nodes$bw1995to2003 ==0 & nodes$bw2003to2013 ==0] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1969 <- NA
tdat$bw1969to1985 <- NA
tdat$bw1985to1995 <- NA
tdat$bw1995to2003 <- NA
tdat$bw2003to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1969[i] <- mean(nodes@data$pre_1969[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1969to1985[i] <- mean(nodes@data$bw1969to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1995[i] <- mean(nodes@data$bw1985to1995[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1995to2003[i] <- mean(nodes@data$bw1995to2003[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2003to2013[i] <- mean(nodes@data$bw2003to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1832 #founding of the town by black quakers from the US (see wikipedia)
tdat$average_age <- ((historical_anchor + ((1969 - historical_anchor) / 2)) * tdat$pre_1969 ) + 
((1969 + ((1985 - 1969) / 2)) * tdat$bw1969to1985 ) + 
((1985 + ((1995 - 1985) / 2)) * tdat$bw1985to1995 ) + 
((1995 + ((2003 - 1995) / 2)) * tdat$bw1995to2003 ) + 
((2003 + ((2013 - 2003) / 2)) * tdat$bw2003to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)


#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="buchanan-1533",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1969
test <- over(boulevards, t1969,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1969 <- 0
boulevards$pre_1969[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1969)

#bw1969to1985
test <- over(boulevards, t1985,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1969to1985 <- 0
boulevards$bw1969to1985[is.na(test$id)==F & boulevards$pre_1969 ==0] <- 1
table(boulevards$pre_1969, boulevards$bw1969to1985)

#bw1985to1995
test <- over(boulevards, t1995,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1995 <- 0
boulevards$bw1985to1995[is.na(test$id)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985==0] <- 1
table(boulevards$bw1969to1985, boulevards$bw1985to1995)

#bw1995to2003
test <- over(boulevards, t2003,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1995to2003 <- 0
boulevards$bw1995to2003[is.na(test$id)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985==0 & boulevards$bw1985to1995 ==0] <- 1
table(boulevards$bw1985to1995, boulevards$bw1995to2003)

#bw2003to2013
test <- over(boulevards, t2013,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2003to2013 <- 0
boulevards$bw2003to2013[is.na(test$id)==F & boulevards$pre_1969 ==0 & boulevards$bw1969to1985==0 & boulevards$bw1985to1995 ==0 & boulevards$bw1995to2003 ==0] <- 1
table(boulevards$bw1995to2003, boulevards$bw2003to2013)

#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1969 ==0 & boulevards$bw1969to1985==0 & boulevards$bw1985to1995 ==0 & boulevards$bw1995to2003 ==0 & boulevards$bw2003to2013 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[9], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "buchanan-1533", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 9: BUJUMBURA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/burundi-BDI_gpkg/bujumbura-4078.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/burundi-BDI_gpkg/bujumbura-4078.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "bujumbura-4078", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "bujumbura-4078", ".Rdata", sep="")	)


#urban extent at 1964
t1964 <- readOGR("./extents/bujumbura/bujumbura1964.shp")
class(t1964)
proj4string(t1964)

#urban extent at 1985
t1985 <- readOGR("./extents/bujumbura/bujumbura1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 2000
t2000 <- readOGR("./extents/bujumbura/bujumbura2000.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2008
t2008 <- readOGR("./extents/bujumbura/bujumbura2008.shp")
class(t2008)
proj4string(t2008)

#urban extent at 2015
t2015 <- readOGR("./extents/bujumbura/bujumbura2015.shp")
class(t2015)
proj4string(t2015)

#pre_1964
test <- over(nodes, t1964)
length(test)
dim(nodes)
head(test)
nodes$pre_1964 <- 0
nodes$pre_1964[is.na(test$id)==F] <- 1
table(test)
table(nodes$pre_1964)

#bw1964to1985
test <- over(nodes, t1985)
length(test)
dim(nodes)
head(test)
nodes$bw1964to1985 <- 0
nodes$bw1964to1985[is.na(test)==F & nodes$pre_1964==0] <- 1
table(test)
table(nodes$pre_1964, nodes$bw1964to1985)

#bw1985to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to2000 <- 0
nodes$bw1985to2000[is.na(test$id)==F & nodes$pre_1964==0 & nodes$bw1964to1985 ==0] <- 1
table(nodes$bw1964to1985, nodes$bw1985to2000)

#bw2000to2008
test <- over(nodes, t2008)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2008 <- 0
nodes$bw2000to2008[is.na(test$id)==F & nodes$pre_1964==0 & nodes$bw1964to1985 ==0 & nodes$bw1985to2000==0] <- 1
table(nodes$bw1985to2000, nodes$bw2000to2008)

#bw2008to2015
test <- over(nodes, t2015)
dim(test)
dim(nodes)
head(test)
nodes$bw2008to2015 <- 0
nodes$bw2008to2015[is.na(test$id)==F & nodes$pre_1964==0 & nodes$bw1964to1985 ==0 & nodes$bw1985to2000==0 & nodes$bw2000to2008 ==0] <- 1
table(nodes$bw2000to2008, nodes$bw2008to2015)

#bw2015to2020
nodes$bw2015to2020 <- 0
nodes$bw2015to2020[ nodes$pre_1964==0 & nodes$bw1964to1985 ==0 & nodes$bw1985to2000==0 & nodes$bw2000to2008 ==0 & nodes$bw2008to2015 ==0] <- 1



### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1964 <- NA
tdat$bw1964to1985 <- NA
tdat$bw1985to2000 <- NA
tdat$bw2000to2008 <- NA
tdat$bw2008to2015 <- NA
tdat$bw2015to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1964[i] <- mean(nodes@data$pre_1964[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1964to1985[i] <- mean(nodes@data$bw1964to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to2000[i] <- mean(nodes@data$bw1985to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2008[i] <- mean(nodes@data$bw2000to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2015[i] <- mean(nodes@data$bw2008to2015[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2015to2020[i] <- mean(nodes@data$bw2015to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1889 #founded as a german military post in 1889
tdat$average_age <- ((historical_anchor + ((1964 - historical_anchor) / 2)) * tdat$pre_1964 ) + 
((1964 + ((1985 - 1964) / 2)) * tdat$bw1964to1985 ) + 
((1985 + ((2000 - 1985) / 2)) * tdat$bw1985to2000 ) + 
((2000 + ((2008 - 2000) / 2)) * tdat$bw2000to2008 ) + 
((2008 + ((2015 - 2008) / 2)) * tdat$bw2008to2015 ) + 
((2015 + ((2020 - 2015) / 2)) * tdat$bw2015to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="bujumbura-4078",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$bw2015to2020!=1] <- dist1[nodes$bw2015to2020!=1,1] 
nodes$dist_to_hq[nodes$bw2015to2020 ==1] <- (dist1[nodes$bw2015to2020 ==1,1] * (3/6)) + (dist2[nodes$bw2015to2020 ==1,1] * (3/6)) #just the time period that is crossed the swithceroo
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1964
test <- over(boulevards, t1964,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1964 <- 0
boulevards$pre_1964[is.na(test$id)==F] <- 1
table(test)
table(boulevards$pre_1964)

#bw1964to1985
test <- over(boulevards, t1985,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$bw1964to1985 <- 0
boulevards$bw1964to1985[is.na(test)==F & boulevards$pre_1964==0] <- 1
table(test)
table(boulevards$pre_1964, boulevards$bw1964to1985)

#bw1985to2000
test <- over(boulevards, t2000,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to2000 <- 0
boulevards$bw1985to2000[is.na(test$id)==F & boulevards$pre_1964==0 & boulevards$bw1964to1985 ==0] <- 1
table(boulevards$bw1964to1985, boulevards$bw1985to2000)

#bw2000to2008
test <- over(boulevards, t2008,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2008 <- 0
boulevards$bw2000to2008[is.na(test$id)==F & boulevards$pre_1964==0 & boulevards$bw1964to1985 ==0 & boulevards$bw1985to2000==0] <- 1
table(boulevards$bw1985to2000, boulevards$bw2000to2008)

#bw2008to2015
test <- over(boulevards, t2015,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2008to2015 <- 0
boulevards$bw2008to2015[is.na(test$id)==F & boulevards$pre_1964==0 & boulevards$bw1964to1985 ==0 & boulevards$bw1985to2000==0 & boulevards$bw2000to2008 ==0] <- 1
table(boulevards$bw2000to2008, boulevards$bw2008to2015)

#bw2015to2020
boulevards$bw2015to2020 <- 0
boulevards$bw2015to2020[ boulevards$pre_1964==0 & boulevards$bw1964to1985 ==0 & boulevards$bw1985to2000==0 & boulevards$bw2000to2008 ==0 & boulevards$bw2008to2015 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[10], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "bujumbura-4078", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 10: CAPE TOWN ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/south_africa-ZAF_gpkg/cape_town-3268.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/south_africa-ZAF_gpkg/cape_town-3268.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "cape_town-3268", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "cape_town-3268", ".Rdata", sep="")	)

#urban extent at 1806
t1806 <- readOGR("./extents/cape_town/capetown1806.shp")
class(t1806)
proj4string(t1806)

#urban extent at 1848
t1848 <- readOGR("./extents/cape_town/capetown1848.shp")
class(t1848)
proj4string(t1848)

#urban extent at 1937
t1937 <- readOGR("./extents/cape_town/capetown1937.shp")
class(t1937)
proj4string(t1937)

#urban extent at 1951
t1951 <- readOGR("./extents/cape_town/capetown1951.shp")
class(t1951)
proj4string(t1951)

#urban extent at 1975
t1975 <- readOGR("./extents/cape_town/capetown1975.shp")
class(t1975)
proj4string(t1975)

#urban extent at 1985
t1985 <- readOGR("./extents/cape_town/capetown1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1994
t1994 <- readOGR("./extents/cape_town/capetown1994.shp")
class(t1994)
proj4string(t1994)

#urban extent at 2001
t2001 <- readOGR("./extents/cape_town/capetown2001.shp")
class(t2001)
proj4string(t2001)

#urban extent at 2012
t2012 <- readOGR("./extents/cape_town/capetown2012.shp")
class(t2012)
proj4string(t2012)


#pre_1806
test <- over(nodes, t1806)
length(test)
dim(nodes)
head(test)
nodes$pre_1806 <- 0
nodes$pre_1806[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1806)

#bw1806to1848
test <- over(nodes, t1848)
dim(test)
dim(nodes)
head(test)
nodes$bw1806to1848 <- 0
nodes$bw1806to1848[is.na(test$id)==F & nodes$pre_1806 ==0] <- 1
table(nodes$pre_1806, nodes$bw1806to1848)

#bw1848to1937
test <- over(nodes, t1937)
dim(test)
dim(nodes)
head(test)
nodes$bw1848to1937 <- 0
nodes$bw1848to1937[is.na(test$id)==F & nodes$pre_1806 ==0 & nodes$bw1806to1848==0] <- 1
table(nodes$bw1806to1848, nodes$bw1848to1937)

#bw1937to1951
test <- over(nodes, t1951)
dim(test)
dim(nodes)
head(test)
nodes$bw1937to1951 <- 0
nodes$bw1937to1951[is.na(test$id)==F  & nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0] <- 1
table(nodes$bw1848to1937, nodes$bw1937to1951)

#bw1951to1975
test <- over(nodes, t1975)
dim(test)
dim(nodes)
head(test)
nodes$bw1951to1975 <- 0
nodes$bw1951to1975[is.na(test$id)==F & nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0 & nodes$bw1937to1951 ==0] <- 1
table(nodes$bw1937to1951, nodes$bw1951to1975)

#bw1975to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1975to1985 <- 0
nodes$bw1975to1985[is.na(test$id)==F  & nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0 & nodes$bw1937to1951 ==0 & nodes$bw1951to1975 ==0] <- 1
table(nodes$bw1951to1975, nodes$bw1975to1985)

#bw1985to1994
test <- over(nodes, t1994)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1994 <- 0
nodes$bw1985to1994[is.na(test$id)==F & nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0 & nodes$bw1937to1951 ==0 & nodes$bw1951to1975 ==0 & nodes$bw1975to1985 ==0] <- 1
table(nodes$bw1975to1985, nodes$bw1985to1994)

#bw1994to2001
test <- over(nodes, t2001)
dim(test)
dim(nodes)
head(test)
nodes$bw1994to2001 <- 0
nodes$bw1994to2001[is.na(test$id)==F & nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0 & nodes$bw1937to1951 ==0 & nodes$bw1951to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1994 ==0] <- 1
table(nodes$bw1985to1994, nodes$bw1994to2001)

#bw2001to2012
test <- over(nodes, t2012)
dim(test)
dim(nodes)
head(test)
nodes$bw2001to2012 <- 0
nodes$bw2001to2012[is.na(test$id)==F  & nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0 & nodes$bw1937to1951 ==0 & nodes$bw1951to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1994 ==0 & nodes$bw1994to2001 ==0] <- 1
table(nodes$bw1994to2001, nodes$bw2001to2012)

#bw2012to2020
nodes$bw2012to2020 <- 0
nodes$bw2012to2020[ nodes$pre_1806 ==0 & nodes$bw1806to1848==0 & nodes$bw1848to1937 ==0 & nodes$bw1937to1951 ==0 & nodes$bw1951to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1994 ==0 & nodes$bw1994to2001 ==0 & nodes$bw2001to2012 ==0] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1806 <- NA
tdat$bw1806to1848 <- NA
tdat$bw1848to1937 <- NA
tdat$bw1937to1951 <- NA
tdat$bw1951to1975 <- NA
tdat$bw1975to1985 <- NA
tdat$bw1985to1994 <- NA
tdat$bw1994to2001 <- NA
tdat$bw2001to2012 <- NA
tdat$bw2012to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1806[i] <- mean(nodes@data$pre_1806[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1806to1848[i] <- mean(nodes@data$bw1806to1848[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1848to1937[i] <- mean(nodes@data$bw1848to1937[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1937to1951[i] <- mean(nodes@data$bw1937to1951[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1951to1975[i] <- mean(nodes@data$bw1951to1975[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1975to1985[i] <- mean(nodes@data$bw1975to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1994[i] <- mean(nodes@data$bw1985to1994[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1994to2001[i] <- mean(nodes@data$bw1994to2001[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2001to2012[i] <- mean(nodes@data$bw2001to2012[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2012to2020[i] <- mean(nodes@data$bw2012to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1652 
tdat$average_age <- ((historical_anchor + ((1806 - historical_anchor) / 2)) * tdat$pre_1806 ) + 
((1806 + ((1848 - 1806) / 2)) * tdat$bw1806to1848 ) + 
((1848 + ((1937 - 1848) / 2)) * tdat$bw1848to1937 ) + 
((1937 + ((1951 - 1937) / 2)) * tdat$bw1937to1951 ) + 
((1951 + ((1975 - 1951) / 2)) * tdat$bw1951to1975 ) + 
((1975 + ((1985 - 1975) / 2)) * tdat$bw1975to1985 ) + 
((1985 + ((1994 - 1985) / 2)) * tdat$bw1985to1994 ) + 
((1994 + ((2001 - 1994) / 2)) * tdat$bw1994to2001 ) + 
((2001 + ((2012 - 2001) / 2)) * tdat$bw2001to2012 ) + 
((2012 + ((2020 - 2012) / 2)) * tdat$bw2012to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="cape_town-3268",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist2[,1]  #only distinction is before time frame of your analyses, so ignoring it here
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['motorway_link', 'motorway']", "['motorway_link', 'primary']", "['motorway_link', 'secondary']", "['motorway', 'trunk']", "['residential', 'primary_link']", "['trunk', 'motorway']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)





#pre_1806
test <- over(boulevards, t1806, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1806 <- 0
boulevards$pre_1806[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1806)

#bw1806to1848
test <- over(boulevards, t1848, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1806to1848 <- 0
boulevards$bw1806to1848[is.na(test$id)==F & boulevards$pre_1806 ==0] <- 1
table(boulevards$pre_1806, boulevards$bw1806to1848)

#bw1848to1937
test <- over(boulevards, t1937, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1848to1937 <- 0
boulevards$bw1848to1937[is.na(test$id)==F & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0] <- 1
table(boulevards$bw1806to1848, boulevards$bw1848to1937)

#bw1937to1951
test <- over(boulevards, t1951, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1937to1951 <- 0
boulevards$bw1937to1951[is.na(test$id)==F  & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0] <- 1
table(boulevards$bw1848to1937, boulevards$bw1937to1951)

#bw1951to1975
test <- over(boulevards, t1975, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1951to1975 <- 0
boulevards$bw1951to1975[is.na(test$id)==F & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0 & boulevards$bw1937to1951 ==0] <- 1
table(boulevards$bw1937to1951, boulevards$bw1951to1975)

#bw1975to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1975to1985 <- 0
boulevards$bw1975to1985[is.na(test$id)==F  & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0 & boulevards$bw1937to1951 ==0 & boulevards$bw1951to1975 ==0] <- 1
table(boulevards$bw1951to1975, boulevards$bw1975to1985)

#bw1985to1994
test <- over(boulevards, t1994, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1994 <- 0
boulevards$bw1985to1994[is.na(test$id)==F & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0 & boulevards$bw1937to1951 ==0 & boulevards$bw1951to1975 ==0 & boulevards$bw1975to1985 ==0] <- 1
table(boulevards$bw1975to1985, boulevards$bw1985to1994)

#bw1994to2001
test <- over(boulevards, t2001, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1994to2001 <- 0
boulevards$bw1994to2001[is.na(test$id)==F & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0 & boulevards$bw1937to1951 ==0 & boulevards$bw1951to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1994 ==0] <- 1
table(boulevards$bw1985to1994, boulevards$bw1994to2001)

#bw2001to2012
test <- over(boulevards, t2012, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2001to2012 <- 0
boulevards$bw2001to2012[is.na(test$id)==F  & boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0 & boulevards$bw1937to1951 ==0 & boulevards$bw1951to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1994 ==0 & boulevards$bw1994to2001 ==0] <- 1
table(boulevards$bw1994to2001, boulevards$bw2001to2012)

#bw2012to2020
boulevards$bw2012to2020 <- 0
boulevards$bw2012to2020[ boulevards$pre_1806 ==0 & boulevards$bw1806to1848==0 & boulevards$bw1848to1937 ==0 & boulevards$bw1937to1951 ==0 & boulevards$bw1951to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1994 ==0 & boulevards$bw1994to2001 ==0 & boulevards$bw2001to2012 ==0] <- 1




#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[11], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" , "ref")]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)
table(is.na(nodes$dist_to_boulevard))

#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km
table(is.na(tdat $avg_dist_to_boulevard))


save(tdat, file=paste("./boeing_data/tcommunities/", "cape_town-3268", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 11: GABORONE ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/botswana-BWA_gpkg/gaborone-3587.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/botswana-BWA_gpkg/gaborone-3587.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "gaborone-3587", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "gaborone-3587", ".Rdata", sep="")	)

#PICK UP FROM HERE....  ..... 

#urban extent at 1970
t1970 <- readOGR("./extents/gaborone/gaborone1970.shp")
class(t1970)
proj4string(t1970)

#urban extent at 1985
t1985 <- readOGR("./extents/gaborone/gaborone1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1991
t1991 <- readOGR("./extents/gaborone/gaborone1991.shp")
class(t1991)
proj4string(t1991)

#urban extent at 2000
t2000 <- readOGR("./extents/gaborone/gaborone2000.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2009
t2009 <- readOGR("./extents/gaborone/gaborone2009.shp")
class(t2009)
proj4string(t2009)

#urban extent at 2015
t2015 <- readOGR("./extents/gaborone/gaborone2015.shp")
class(t2015)
proj4string(t2015)


#pre_1970
test <- over(nodes, t1970)
length(test)
dim(nodes)
head(test)
nodes$pre_1970 <- 0
nodes$pre_1970[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1970)


#bw1970to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1970to1985 <- 0
nodes$bw1970to1985[is.na(test$id)==F & nodes$pre_1970 ==0] <- 1
table(nodes$pre_1970, nodes$bw1970to1985)


#bw1985to1991
test <- over(nodes, t1991)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1991 <- 0
nodes$bw1985to1991[is.na(test$id)==F & nodes$pre_1970 ==0 & nodes$bw1970to1985 ==0] <- 1
table(nodes$bw1970to1985, nodes$bw1985to1991)


#bw1991to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1991to2000 <- 0
nodes$bw1991to2000[is.na(test$id)==F & nodes$pre_1970 ==0 & nodes$bw1970to1985 ==0 & nodes$bw1985to1991 ==0] <- 1
table(nodes$bw1985to1991, nodes$bw1991to2000)


#bw2000to2009
test <- over(nodes, t2009)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2009 <- 0
nodes$bw2000to2009[is.na(test$id)==F & nodes$pre_1970 ==0 & nodes$bw1970to1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2000 ==0] <- 1
table(nodes$bw1991to2000, nodes$bw2000to2009)

#bw2009to2015
test <- over(nodes, t2015)
dim(test)
dim(nodes)
head(test)
nodes$bw2009to2015 <- 0
nodes$bw2009to2015[is.na(test$id)==F & nodes$pre_1970 ==0 & nodes$bw1970to1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2000 ==0 & nodes$bw2000to2009 ==0] <- 1
table(nodes$bw2000to2009, nodes$bw2009to2015)

#bw2015to2020
nodes$bw2015to2020 <- 0
nodes$bw2015to2020[  nodes$pre_1970 ==0 & nodes$bw1970to1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2000 ==0 & nodes$bw2000to2009 ==0 & nodes$bw2009to2015 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1970 <- NA
tdat$bw1970to1985 <- NA
tdat$bw1985to1991 <- NA
tdat$bw1991to2000 <- NA
tdat$bw2000to2009 <- NA
tdat$bw2009to2015 <- NA
tdat$bw2015to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1970[i] <- mean(nodes@data$pre_1970[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1970to1985[i] <- mean(nodes@data$bw1970to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1991[i] <- mean(nodes@data$bw1985to1991[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1991to2000[i] <- mean(nodes@data$bw1991to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2009[i] <- mean(nodes@data$bw2000to2009[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2009to2015[i] <- mean(nodes@data$bw2009to2015[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2015to2020[i] <- mean(nodes@data$bw2015to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1964 #1880 #date of first major settlement by the Tlokwa (wikipedia)
tdat$average_age <- ((historical_anchor + ((1970 - historical_anchor) / 2)) * tdat$pre_1970 ) + 
((1970 + ((1985 - 1970) / 2)) * tdat$bw1970to1985 ) + 
((1985 + ((1991 - 1985) / 2)) * tdat$bw1985to1991 ) + 
((1991 + ((2000 - 1991) / 2)) * tdat$bw1991to2000 ) + 
((2000 + ((2009 - 2000) / 2)) * tdat$bw2000to2009 ) + 
((2009 + ((2015 - 2009) / 2)) * tdat$bw2009to2015 ) + 
((2015+ ((2020 - 2015) / 2)) * tdat$bw2015to2020 ) 
summary(tdat$average_age)


#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="gaborone-3587",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)



#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1970
test <- over(boulevards, t1970,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1970 <- 0
boulevards$pre_1970[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1970)


#bw1970to1985
test <- over(boulevards, t1985,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1970to1985 <- 0
boulevards$bw1970to1985[is.na(test$id)==F & boulevards$pre_1970 ==0] <- 1
table(boulevards$pre_1970, boulevards$bw1970to1985)


#bw1985to1991
test <- over(boulevards, t1991,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1991 <- 0
boulevards$bw1985to1991[is.na(test$id)==F & boulevards$pre_1970 ==0 & boulevards$bw1970to1985 ==0] <- 1
table(boulevards$bw1970to1985, boulevards$bw1985to1991)


#bw1991to2000
test <- over(boulevards, t2000,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1991to2000 <- 0
boulevards$bw1991to2000[is.na(test$id)==F & boulevards$pre_1970 ==0 & boulevards$bw1970to1985 ==0 & boulevards$bw1985to1991 ==0] <- 1
table(boulevards$bw1985to1991, boulevards$bw1991to2000)


#bw2000to2009
test <- over(boulevards, t2009,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2009 <- 0
boulevards$bw2000to2009[is.na(test$id)==F & boulevards$pre_1970 ==0 & boulevards$bw1970to1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2000 ==0] <- 1
table(boulevards$bw1991to2000, boulevards$bw2000to2009)

#bw2009to2015
test <- over(boulevards, t2015,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2009to2015 <- 0
boulevards$bw2009to2015[is.na(test$id)==F & boulevards$pre_1970 ==0 & boulevards$bw1970to1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2000 ==0 & boulevards$bw2000to2009 ==0] <- 1
table(boulevards$bw2000to2009, boulevards$bw2009to2015)

#bw2015to2020
boulevards$bw2015to2020 <- 0
boulevards$bw2015to2020[  boulevards$pre_1970 ==0 & boulevards$bw1970to1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2000 ==0 & boulevards$bw2000to2009 ==0 & boulevards$bw2009to2015 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[12], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "gaborone-3587", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 12: GOMBE ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/gombe-2932.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/gombe-2932.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "gombe-2932", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "gombe-2932", ".Rdata", sep="")	)


#urban extent at 1967
t1967 <- readOGR("./extents/gombe/gombe1967.shp")
class(t1967)
proj4string(t1967)

#urban extent at 1985
t1985 <- readOGR("./extents/gombe/gombe1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1990
t1990 <- readOGR("./extents/gombe/urbFootprint_t1_subset_pruned.shp")
class(t1990)
proj4string(t1990)

#urban extent at 2000
t2000 <- readOGR("./extents/gombe/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2009
t2009 <- readOGR("./extents/gombe/gombe2009.shp")
class(t2009)
proj4string(t2009)

#urban extent at 2013
t2013 <- readOGR("./extents/gombe/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)

#pre_1967
test <- over(nodes, t1967)
length(test)
dim(nodes)
head(test)
nodes$pre_1967 <- 0
nodes$pre_1967[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1967)

#bw1967to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1967to1985 <- 0
nodes$bw1967to1985[is.na(test$id)==F & nodes$pre_1967 ==0] <- 1
table(nodes$pre_1967, nodes$bw1967to1985)

#bw1985to1990  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1990 <- 0
nodes$bw1985to1990[is.na(test$fid)==F & nodes$pre_1967 ==0 & nodes$bw1967to1985 == 0] <- 1
table(nodes$bw1967to1985, nodes$bw1985to1990)

#bw1990to2000  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to2000 <- 0
nodes$bw1990to2000[is.na(test$fid)==F & nodes$pre_1967 ==0 & nodes$bw1967to1985 ==0 & nodes$bw1985to1990 == 0] <- 1
table(nodes$bw1985to1990, nodes$bw1990to2000)

#bw2000to2009
test <- over(nodes, t2009)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2009 <- 0
nodes$bw2000to2009[is.na(test$id)==F & nodes$pre_1967 ==0 & nodes$bw1967to1985 ==0 & nodes$bw1985to1990 == 0 & nodes$bw1990to2000 ==0] <- 1
table(nodes$bw1990to2000, nodes$bw2000to2009)


#bw2009to2013  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2009to2013 <- 0
nodes$bw2009to2013[is.na(test$fid)==F & nodes$pre_1967 ==0 & nodes$bw1967to1985 ==0 & nodes$bw1985to1990 == 0 & nodes$bw1990to2000 ==0 & nodes$bw2000to2009 == 0] <- 1
table(nodes$bw2000to2009, nodes$bw2009to2013)


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1967 ==0 & nodes$bw1967to1985 ==0 & nodes$bw1985to1990 == 0 & nodes$bw1990to2000 ==0 & nodes$bw2000to2009 == 0 & nodes$bw2009to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1967 <- NA
tdat$bw1967to1985 <- NA
tdat$bw1985to1990 <- NA
tdat$bw1990to2000 <- NA
tdat$bw2000to2009 <- NA
tdat$bw2009to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1967[i] <- mean(nodes@data$pre_1967[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1967to1985[i] <- mean(nodes@data$bw1967to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1990[i] <- mean(nodes@data$bw1985to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to2000[i] <- mean(nodes@data$bw1990to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2009[i] <- mean(nodes@data$bw2000to2009[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2009to2013[i] <- mean(nodes@data$bw2009to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1824 #https://www.britannica.com/place/Gombe-emirate-Nigeria
tdat$average_age <- ((historical_anchor + ((1967 - historical_anchor) / 2)) * tdat$pre_1967 ) + 
((1967 + ((1985 - 1967) / 2)) * tdat$bw1967to1985 ) + 
((1985 + ((1990 - 1985) / 2)) * tdat$bw1985to1990 ) + 
((1990 + ((2000 - 1990) / 2)) * tdat$bw1990to2000 ) + 
((2000 + ((2009 - 2000) / 2)) * tdat$bw2000to2009 ) + 
((2009 + ((2013 - 2009) / 2)) * tdat$bw2009to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="gombe-2932",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)



#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1967
test <- over(boulevards, t1967, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1967 <- 0
boulevards$pre_1967[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1967)

#bw1967to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1967to1985 <- 0
boulevards$bw1967to1985[is.na(test$id)==F & boulevards$pre_1967 ==0] <- 1
table(boulevards$pre_1967, boulevards$bw1967to1985)

#bw1985to1990  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1990 <- 0
boulevards$bw1985to1990[is.na(test$fid)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1985 == 0] <- 1
table(boulevards$bw1967to1985, boulevards$bw1985to1990)

#bw1990to2000  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to2000 <- 0
boulevards$bw1990to2000[is.na(test$fid)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1985 ==0 & boulevards$bw1985to1990 == 0] <- 1
table(boulevards$bw1985to1990, boulevards$bw1990to2000)

#bw2000to2009
test <- over(boulevards, t2009, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2009 <- 0
boulevards$bw2000to2009[is.na(test$id)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1985 ==0 & boulevards$bw1985to1990 == 0 & boulevards$bw1990to2000 ==0] <- 1
table(boulevards$bw1990to2000, boulevards$bw2000to2009)


#bw2009to2013  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2013, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2009to2013 <- 0
boulevards$bw2009to2013[is.na(test$fid)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1985 ==0 & boulevards$bw1985to1990 == 0 & boulevards$bw1990to2000 ==0 & boulevards$bw2000to2009 == 0] <- 1
table(boulevards$bw2000to2009, boulevards$bw2009to2013)


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1967 ==0 & boulevards$bw1967to1985 ==0 & boulevards$bw1985to1990 == 0 & boulevards$bw1990to2000 ==0 & boulevards$bw2000to2009 == 0 & boulevards$bw2009to2013 ==0] <- 1




#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[13], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "gombe-2932", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 13: IBADAN ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/ibadan-2189.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/ibadan-2189.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "ibadan-2189", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "ibadan-2189", ".Rdata", sep="")	)

#urban extent at 1949
t1949 <- readOGR("./extents/ibadan/Merged Ibadan 1949.shp")
class(t1949)
proj4string(t1949)

#urban extent at 1960
t1960 <- readOGR("./extents/ibadan/Merged 1960.shp")
class(t1960)
proj4string(t1960)

#urban extent at 1974
t1974 <- readOGR("./extents/ibadan/ibadan1974.shp")
class(t1974)
proj4string(t1974)

#urban extent at 1978
t1978 <- readOGR("./extents/ibadan/ibadan1978.shp")
class(t1978)
proj4string(t1978)

#urban extent at 1984
t1984 <- readOGR("./extents/ibadan/urbFootprint_t1_subset_pruned.shp")
class(t1984)
proj4string(t1984)

#urban extent at 2000
t2000 <- readOGR("./extents/ibadan/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2007
t2007 <- readOGR("./extents/ibadan/ibadan2007.shp")
class(t2007)
proj4string(t2007)

#urban extent at 2013
t2013 <- readOGR("./extents/ibadan/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)

#pre_1949
t1949@data <- as.data.frame(matrix(1, nrow=nrow(t1949), ncol=1))
test <- over(nodes, t1949)
length(test)
dim(nodes)
head(test)
nodes$pre_1949 <- 0
nodes$pre_1949[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1949)

#bw1949to1960
t1960@data <- as.data.frame(matrix(1, nrow=nrow(t1960), ncol=1))
test <- over(nodes, t1960)
dim(test)
dim(nodes)
head(test)
nodes$bw1949to1960 <- 0
nodes$bw1949to1960[is.na(test$V1)==F & nodes$pre_1949 ==0] <- 1
table(nodes$pre_1949, nodes$bw1949to1960)

#bw1960to1974
test <- over(nodes, t1974)
dim(test)
dim(nodes)
head(test)
nodes$bw1960to1974 <- 0
nodes$bw1960to1974[is.na(test$id)==F & nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0] <- 1
table(nodes$bw1949to1960, nodes$bw1960to1974)


#bw1974to1978
test <- over(nodes, t1978)
dim(test)
dim(nodes)
head(test)
nodes$bw1974to1978 <- 0
nodes$bw1974to1978[is.na(test$id)==F & nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0 & nodes$bw1960to1974 ==0] <- 1
table(nodes$bw1960to1974, nodes$bw1974to1978)


#bw1978to1984  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t1984)
dim(test)
dim(nodes)
head(test)
nodes$bw1978to1984 <- 0
nodes$bw1978to1984[is.na(test$fid)==F & nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0 & nodes$bw1960to1974 ==0 & nodes$bw1974to1978 == 0] <- 1
table(nodes$bw1974to1978, nodes$bw1978to1984)


#bw1984to2000  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1984to2000 <- 0
nodes$bw1984to2000[is.na(test$fid)==F & nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0 & nodes$bw1960to1974 ==0 & nodes$bw1974to1978 == 0 & nodes$bw1978to1984 == 0] <- 1
table(nodes$bw1978to1984, nodes$bw1984to2000)


#bw2000to2007
test <- over(nodes, t2007)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2007 <- 0
nodes$bw2000to2007[is.na(test$id)==F & nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0 & nodes$bw1960to1974 ==0 & nodes$bw1974to1978 == 0 & nodes$bw1978to1984 == 0 & nodes$bw1984to2000 ==0] <- 1
table(nodes$bw1984to2000, nodes$bw2000to2007)


#bw2007to2013  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2007to2013 <- 0
nodes$bw2007to2013[is.na(test$fid)==F & nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0 & nodes$bw1960to1974 ==0 & nodes$bw1974to1978 == 0 & nodes$bw1978to1984 == 0 & nodes$bw1984to2000 ==0 & nodes$bw2000to2007 == 0] <- 1
table(nodes$bw2000to2007, nodes$bw2007to2013)

#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1949 ==0 & nodes$bw1949to1960 ==0 & nodes$bw1960to1974 ==0 & nodes$bw1974to1978 == 0 & nodes$bw1978to1984 == 0 & nodes$bw1984to2000 ==0 & nodes$bw2000to2007 == 0 & nodes$bw2007to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1949 <- NA
tdat$bw1949to1960 <- NA
tdat$bw1960to1974 <- NA
tdat$bw1974to1978 <- NA
tdat$bw1978to1984 <- NA
tdat$bw1984to2000 <- NA
tdat$bw2000to2007 <- NA
tdat$bw2007to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1949[i] <- mean(nodes@data$pre_1949[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1949to1960[i] <- mean(nodes@data$bw1949to1960[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1960to1974[i] <- mean(nodes@data$bw1960to1974[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1974to1978[i] <- mean(nodes@data$bw1974to1978[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1978to1984[i] <- mean(nodes@data$bw1978to1984[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1984to2000[i] <- mean(nodes@data$bw1984to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2007[i] <- mean(nodes@data$bw2000to2007[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2007to2013[i] <- mean(nodes@data$bw2007to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1829 #wikipedia date for city's founding
tdat$average_age <- ((historical_anchor + ((1949 - historical_anchor) / 2)) * tdat$pre_1949 ) + 
((1949 + ((1960 - 1949) / 2)) * tdat$bw1949to1960 ) + 
((1960 + ((1974 - 1960) / 2)) * tdat$bw1960to1974 ) + 
((1974 + ((1978 - 1974) / 2)) * tdat$bw1974to1978 ) + 
((1978 + ((1984 - 1978) / 2)) * tdat$bw1978to1984 ) + 
((1984 + ((2000 - 1984) / 2)) * tdat$bw1984to2000 ) + 
((2000 + ((2007 - 2000) / 2)) * tdat$bw2000to2007 ) + 
((2007 + ((2013 - 2007) / 2)) * tdat$bw2007to2013 ) +
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="ibadan-2189",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


test <- over(boulevards, t1949, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1949 <- 0
boulevards$pre_1949[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1949)

#bw1949to1960
t1960@data <- as.data.frame(matrix(1, nrow=nrow(t1960), ncol=1))
test <- over(boulevards, t1960, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1949to1960 <- 0
boulevards$bw1949to1960[is.na(test$V1)==F & boulevards$pre_1949 ==0] <- 1
table(boulevards$pre_1949, boulevards$bw1949to1960)

#bw1960to1974
test <- over(boulevards, t1974, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1960to1974 <- 0
boulevards$bw1960to1974[is.na(test$id)==F & boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0] <- 1
table(boulevards$bw1949to1960, boulevards$bw1960to1974)


#bw1974to1978
test <- over(boulevards, t1978, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1974to1978 <- 0
boulevards$bw1974to1978[is.na(test$id)==F & boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0 & boulevards$bw1960to1974 ==0] <- 1
table(boulevards$bw1960to1974, boulevards$bw1974to1978)


#bw1978to1984  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t1984, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1978to1984 <- 0
boulevards$bw1978to1984[is.na(test$fid)==F & boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0 & boulevards$bw1960to1974 ==0 & boulevards$bw1974to1978 == 0] <- 1
table(boulevards$bw1974to1978, boulevards$bw1978to1984)


#bw1984to2000  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1984to2000 <- 0
boulevards$bw1984to2000[is.na(test$fid)==F & boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0 & boulevards$bw1960to1974 ==0 & boulevards$bw1974to1978 == 0 & boulevards$bw1978to1984 == 0] <- 1
table(boulevards$bw1978to1984, boulevards$bw1984to2000)


#bw2000to2007
test <- over(boulevards, t2007, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2007 <- 0
boulevards$bw2000to2007[is.na(test$id)==F & boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0 & boulevards$bw1960to1974 ==0 & boulevards$bw1974to1978 == 0 & boulevards$bw1978to1984 == 0 & boulevards$bw1984to2000 ==0] <- 1
table(boulevards$bw1984to2000, boulevards$bw2000to2007)


#bw2007to2013  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2013, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2007to2013 <- 0
boulevards$bw2007to2013[is.na(test$fid)==F & boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0 & boulevards$bw1960to1974 ==0 & boulevards$bw1974to1978 == 0 & boulevards$bw1978to1984 == 0 & boulevards$bw1984to2000 ==0 & boulevards$bw2000to2007 == 0] <- 1
table(boulevards$bw2000to2007, boulevards$bw2007to2013)

#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1949 ==0 & boulevards$bw1949to1960 ==0 & boulevards$bw1960to1974 ==0 & boulevards$bw1974to1978 == 0 & boulevards$bw1978to1984 == 0 & boulevards$bw1984to2000 ==0 & boulevards$bw2000to2007 == 0 & boulevards$bw2007to2013 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[14], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" , "ref")]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)
table(is.na(nodes$dist_to_boulevard))

#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km
table(is.na(tdat$avg_dist_to_boulevard))


save(tdat, file=paste("./boeing_data/tcommunities/", "ibadan-2189", "_tdat_wyears_NEW", ".Rdata", sep="")	)

#####################################
##### CITY 14: JOHANNESBURG ######

### note: jo'burg takes so long to run that i have to cut up the process into smaller chunks below and then stitch it back together
### this city alone may take a few days for a standard computer


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/south_africa-ZAF_gpkg/johannesburg-3673.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/south_africa-ZAF_gpkg/johannesburg-3673.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "johannesburg-3673", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "johannesburg-3673", ".Rdata", sep="")	)

#urban extent at 1934
t1934 <- readOGR("./extents/johannesburg/johannesburg1934.shp")
class(t1934)
proj4string(t1934)

#urban extent at 1964
t1964 <- readOGR("./extents/johannesburg/Joburg 1964 LOC.shp")
class(t1964)
proj4string(t1964)
t1964@data <- as.data.frame(matrix(1, nrow=nrow(t1964), ncol=1))
colnames(t1964@data) <- c("id")

#urban extent at 1975
t1975 <- readOGR("./extents/johannesburg/johannesburg1975.shp")
class(t1975)
proj4string(t1975)

#urban extent at 1985
t1985 <- readOGR("./extents/johannesburg/johannesburg1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1990
t1990 <- readOGR("./extents/johannesburg/urbFootprint_t1_subset_pruned.shp")
class(t1990)
proj4string(t1990)

#urban extent at 1998
t1998 <- readOGR("./extents/johannesburg/urbFootprint_t2_subset_pruned.shp")
class(t1998)
proj4string(t1998)

#urban extent at 2013
t2013 <- readOGR("./extents/johannesburg/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)



#pre_1934
test <- over(nodes, t1934)
length(test)
dim(nodes)
head(test)
nodes$pre_1934 <- 0
nodes$pre_1934[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1934)

#bw1934to1964
test <- over(nodes, t1964)
dim(test)
dim(nodes)
head(test)
nodes$bw1934to1964 <- 0
nodes$bw1934to1964[is.na(test$id)==F & nodes$pre_1934 ==0] <- 1
table(nodes$pre_1934, nodes$bw1934to1964)

#bw1964to1975
test <- over(nodes, t1975)
dim(test)
dim(nodes)
head(test)
nodes$bw1964to1975 <- 0
nodes$bw1964to1975[is.na(test$id)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0] <- 1
table(nodes$bw1934to1964, nodes$bw1964to1975)

#bw1975to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1975to1985 <- 0
nodes$bw1975to1985[is.na(test$id)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0] <- 1
table(nodes$bw1964to1975, nodes$bw1975to1985)

#bw1985to1990
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1990 <- 0
nodes$bw1985to1990[is.na(test$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0] <- 1
table(nodes$bw1975to1985, nodes$bw1985to1990)

#bw1990to1998
test <- over(nodes, t1998)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to1998 <- 0
nodes$bw1990to1998[is.na(test$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0] <- 1
table(nodes$bw1985to1990, nodes$bw1990to1998)

#bw1998to2013
test <- NA
t1990 <- NA
t1998 <- NA
test1 <- over(nodes, t2013[1:round((nrow(t2013)/2)),])
nodes$bw1998to2013 <- 0
nodes$bw1998to2013[is.na(test1$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
test1 <- NA
test2 <- over(nodes, t2013[(round(nrow(t2013)/2)): 95758,])
nodes$bw1998to2013[is.na(test2$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
test2 <- NA
test3 <- over(nodes, t2013[95760:nrow(t2013),])
nodes$bw1998to2013[is.na(test3$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1

#item 95759 has a bad geometry. this was fixed manually in gqis. now you will run the overlay just for that one and then finish. 
#urban extent at 2013

t2013A <- readOGR("./extents/johannesburg/temp_fixed1.shp")
class(t2013A)
proj4string(t2013A)

test4 <- over(nodes, t2013A)
nodes$bw1998to2013[is.na(test4$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2013)
test4 <- NA

t2013B <- readOGR("./extents/johannesburg/temp_fixed2.shp")
t2013C <- readOGR("./extents/johannesburg/temp_fixed3.shp")
t2013D <- readOGR("./extents/johannesburg/temp_fixed4.shp")
t2013E <- readOGR("./extents/johannesburg/temp_fixed5.shp")

test4 <- over(nodes, t2013B)
nodes$bw1998to2013[is.na(test4$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2013)
test4 <- NA
test4 <- over(nodes, t2013C)
nodes$bw1998to2013[is.na(test4$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2013)
test4 <- NA
test4 <- over(nodes, t2013D)
nodes$bw1998to2013[is.na(test4$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2013)
test4 <- NA
test4 <- over(nodes, t2013E)
nodes$bw1998to2013[is.na(test4$fid)==F & nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2013)
test4 <- NA


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1934 ==0 & nodes$bw1934to1964 ==0 & nodes$bw1964to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0 & nodes$bw1998to2013 ==0] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1934 <- NA
tdat$bw1934to1964 <- NA
tdat$bw1964to1975 <- NA
tdat$bw1975to1985 <- NA
tdat$bw1985to1990 <- NA
tdat$bw1990to1998 <- NA
tdat$bw1998to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1934[i] <- mean(nodes@data$pre_1934[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1934to1964[i] <- mean(nodes@data$bw1934to1964[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1964to1975[i] <- mean(nodes@data$bw1964to1975[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1975to1985[i] <- mean(nodes@data$bw1975to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1990[i] <- mean(nodes@data$bw1985to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to1998[i] <- mean(nodes@data$bw1990to1998[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1998to2013[i] <- mean(nodes@data$bw1998to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1886 #wikipedia date for city's founding
tdat$average_age <- ((historical_anchor + ((1934 - historical_anchor) / 2)) * tdat$pre_1934 ) + 
((1934 + ((1964 - 1934) / 2)) * tdat$bw1934to1964 ) + 
((1964 + ((1975 - 1964) / 2)) * tdat$bw1964to1975 ) + 
((1975 + ((1985 - 1975) / 2)) * tdat$bw1975to1985 ) + 
((1985 + ((1990 - 1985) / 2)) * tdat$bw1985to1990 ) + 
((1990 + ((1998 - 1990) / 2)) * tdat$bw1990to1998 ) + 
((1998 + ((2013 - 1998) / 2)) * tdat$bw1998to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="johannesburg-3673",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['motorway_link', 'motorway']", "['motorway_link', 'trunk']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1934
test <- over(boulevards, t1934, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1934 <- 0
boulevards$pre_1934[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1934)

#bw1934to1964
test <- over(boulevards, t1964, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1934to1964 <- 0
boulevards$bw1934to1964[is.na(test$id)==F & boulevards$pre_1934 ==0] <- 1
table(boulevards$pre_1934, boulevards$bw1934to1964)

#bw1964to1975
test <- over(boulevards, t1975, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1964to1975 <- 0
boulevards$bw1964to1975[is.na(test$id)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0] <- 1
table(boulevards$bw1934to1964, boulevards$bw1964to1975)

#bw1975to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1975to1985 <- 0
boulevards$bw1975to1985[is.na(test$id)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0] <- 1
table(boulevards$bw1964to1975, boulevards$bw1975to1985)

#urban extent at 1990
t1990 <- readOGR("./extents/johannesburg/urbFootprint_t1_subset_pruned.shp")
class(t1990)
proj4string(t1990)

#urban extent at 1998
t1998 <- readOGR("./extents/johannesburg/urbFootprint_t2_subset_pruned.shp")
class(t1998)
proj4string(t1998)

#bw1985to1990
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1990 <- 0
boulevards$bw1985to1990[is.na(test$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0] <- 1
table(boulevards$bw1975to1985, boulevards$bw1985to1990)

#bw1990to1998
test <- over(boulevards, t1998, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to1998 <- 0
boulevards$bw1990to1998[is.na(test$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0] <- 1
table(boulevards$bw1985to1990, boulevards$bw1990to1998)

#bw1998to2013
test <- NA
t1990 <- NA
t1998 <- NA
t2013_update <- gBuffer(t2013, byid=TRUE, width=0)

test1 <- over(boulevards, t2013_update[1:round((nrow(t2013_update)/2)),], returnList=F)
boulevards$bw1998to2013 <- 0
boulevards$bw1998to2013[is.na(test1$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
test1 <- NA
test2 <- over(boulevards, t2013_update[(round(nrow(t2013_update)/2)): 95758,], returnList=F)
boulevards$bw1998to2013[is.na(test2$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
test2 <- NA
test3 <- over(boulevards, t2013_update[95760:nrow(t2013_update),], returnList=F)
boulevards$bw1998to2013[is.na(test3$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1

#item 95759 has a bad geometry. this was fixed manually in gqis. now you will run the overlay just for that one and then finish. 
#urban extent at 2013

t2013A <- readOGR("./extents/johannesburg/temp_fixed1.shp")
class(t2013A)
proj4string(t2013A)

test4 <- over(boulevards, t2013A, returnList=F)
boulevards$bw1998to2013[is.na(test4$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2013)
test4 <- NA

t2013B <- readOGR("./extents/johannesburg/temp_fixed2.shp")
t2013C <- readOGR("./extents/johannesburg/temp_fixed3.shp")
t2013D <- readOGR("./extents/johannesburg/temp_fixed4.shp")
t2013E <- readOGR("./extents/johannesburg/temp_fixed5.shp")

test4 <- over(boulevards, t2013B, returnList=F)
boulevards$bw1998to2013[is.na(test4$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2013)
test4 <- NA
test4 <- over(boulevards, t2013C, returnList=F)
boulevards$bw1998to2013[is.na(test4$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2013)
test4 <- NA
test4 <- over(boulevards, t2013D, returnList=F)
boulevards$bw1998to2013[is.na(test4$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2013)
test4 <- NA
test4 <- over(boulevards, t2013E, returnList=F)
boulevards$bw1998to2013[is.na(test4$fid)==F & boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2013)
test4 <- NA


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1934 ==0 & boulevards$bw1934to1964 ==0 & boulevards$bw1964to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0 & boulevards$bw1998to2013 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[15], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard", "ref" )]


### check that there isn't something missing here. 


#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
#summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}

if(i < 4){ #if i is less than 5, continue the normal way
		
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

} #end i < 5 loop


if(i >= 4){ #if i is more than 5, cut it up into sections

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#cut nodes2 into 1/10ths and do it 10x separately
	
cutoffs <- as.vector(round(quantile(1:nrow(nodes2), c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)), digits=0))

nodes2.1 <- nodes2[1:cutoffs[1],]	
#gml2.1 <- subgraph(gml2, V(gml2)$id %in% nodes2.1@data$osmid) #can't do this because the distances through the network will be wrong
#summary(gml2.1)
	#need to keep the gml object as it is; but can do the nodes object smaller

nodes2.2 <- nodes2[(cutoffs[1]+1):cutoffs[2],]	
nodes2.3 <- nodes2[(cutoffs[2]+1):cutoffs[3],]	
nodes2.4 <- nodes2[(cutoffs[3]+1):cutoffs[4],]	
nodes2.5 <- nodes2[(cutoffs[4]+1):cutoffs[5],]	
nodes2.6 <- nodes2[(cutoffs[5]+1):cutoffs[6],]	
nodes2.7 <- nodes2[(cutoffs[6]+1):cutoffs[7],]	
nodes2.8 <- nodes2[(cutoffs[7]+1):cutoffs[8],]	
nodes2.9 <- nodes2[(cutoffs[8]+1):cutoffs[9],]	
nodes2.10 <- nodes2[(cutoffs[9]+1):nrow(nodes2),]	


#PASS 1: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.1 <- over(nodes2.1, boulevards2, returnList=F)
nodes3.1 <- nodes2.1[is.na(dists1.1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.1	<- vertex.ids[vertex.ids[,1] %in% nodes3.1$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.1 <- distances(gml2, to=nodes.to.1, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.1 <- dists2.1[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.1 <- NA
dists2.1 <- NA
nodes2.1 <- NA

#PASS 2: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.2 <- over(nodes2.2, boulevards2, returnList=F)
nodes3.2 <- nodes2.2[is.na(dists1.2 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.2	<- vertex.ids[vertex.ids[,1] %in% nodes3.2$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.2 <- distances(gml2, to=nodes.to.2, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.2 <- dists2.2[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these	

dists1.2 <- NA
dists2.2 <- NA
nodes2.2 <- NA
	
#PASS 3: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.3 <- over(nodes2.3, boulevards2, returnList=F)
nodes3.3 <- nodes2.3[is.na(dists1.3 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.3	<- vertex.ids[vertex.ids[,1] %in% nodes3.3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.3 <- distances(gml2, to=nodes.to.3, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.3 <- dists2.3[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.3 <- NA
dists2.3 <- NA
nodes2.3 <- NA

#PASS 4: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.4 <- over(nodes2.4, boulevards2, returnList=F)
nodes3.4 <- nodes2.4[is.na(dists1.4 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.4	<- vertex.ids[vertex.ids[,1] %in% nodes3.4$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.4 <- distances(gml2, to=nodes.to.4, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.4 <- dists2.4[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.4 <- NA
dists2.4 <- NA
nodes2.4 <- NA


#PASS 5: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.5 <- over(nodes2.5, boulevards2, returnList=F)
nodes3.5 <- nodes2.5[is.na(dists1.5 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.5	<- vertex.ids[vertex.ids[,1] %in% nodes3.5$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.5 <- distances(gml2, to=nodes.to.5, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.5 <- dists2.5[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.5 <- NA
dists2.5 <- NA
nodes2.5 <- NA


#PASS 6: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.6 <- over(nodes2.6, boulevards2, returnList=F)
nodes3.6 <- nodes2.6[is.na(dists1.6 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.6	<- vertex.ids[vertex.ids[,1] %in% nodes3.6$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.6 <- distances(gml2, to=nodes.to.6, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.6 <- dists2.6[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.6 <- NA
dists2.6 <- NA
nodes2.6 <- NA


#PASS 7: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.7 <- over(nodes2.7, boulevards2, returnList=F)
nodes3.7 <- nodes2.7[is.na(dists1.7 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.7	<- vertex.ids[vertex.ids[,1] %in% nodes3.7$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.7 <- distances(gml2, to=nodes.to.7, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.7 <- dists2.7[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.7 <- NA
dists2.7 <- NA
nodes2.7 <- NA


#PASS 8: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.8 <- over(nodes2.8, boulevards2, returnList=F)
nodes3.8 <- nodes2.8[is.na(dists1.8 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.8	<- vertex.ids[vertex.ids[,1] %in% nodes3.8$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.8 <- distances(gml2, to=nodes.to.8, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.8 <- dists2.8[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.8 <- NA
dists2.8 <- NA
nodes2.8 <- NA

#PASS 9: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.9 <- over(nodes2.9, boulevards2, returnList=F)
nodes3.9 <- nodes2.9[is.na(dists1.9 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.9	<- vertex.ids[vertex.ids[,1] %in% nodes3.9$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.9 <- distances(gml2, to=nodes.to.9, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.9 <- dists2.9[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these

dists1.9 <- NA
dists2.9 <- NA
nodes2.9 <- NA

#PASS 10: 

#find nodes that intersect the boulevards; these become the "target" nodes
dists1.10 <- over(nodes2.10, boulevards2, returnList=F)
nodes3.10 <- nodes2.10[is.na(dists1.10 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to.10	<- vertex.ids[vertex.ids[,1] %in% nodes3.10$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2.10 <- distances(gml2, to=nodes.to.10, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the dists object just to the nodes in *THIS* band
dists2a.10 <- dists2.10[nodes2$osmid %in% nodes4$osmid,]
#you will append the columns to each other for these	

dists1.10 <- NA
dists2.10 <- NA
nodes2.10 <- NA

dists2a <- cbind(dists2a.1, dists2a.2, dists2a.3, dists2a.4, dists2a.5, dists2a.6, dists2a.7, dists2a.8, dists2a.9, dists2a.10)

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away
		
}	 #if statement for iterating over 10 sections for i >= 5




#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

#Error: vector memory exhausted (limit reached?)
	#jesus... 

summary(nodes$dist_to_boulevard)
table(is.na(nodes$dist_to_boulevard))

#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "johannesburg-3673", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 15: KAMPALA ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/uganda-UGA_gpkg/kampala-4427.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/uganda-UGA_gpkg/kampala-4427.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "kampala-4427", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "kampala-4427", ".Rdata", sep="")	)


#urban extent at 1963
t1963 <- readOGR("./extents/kampala/kampala1963.shp")
class(t1963)
proj4string(t1963)

#urban extent at 1988
t1988 <- readOGR("./extents/kampala/urbFootprint_t1_subset_pruned.shp")
class(t1988)
proj4string(t1988)

#urban extent at 1996
t1996 <- readOGR("./extents/kampala/kampala1996.shp")
class(t1996)
proj4string(t1996)

#urban extent at 2003
t2003 <- readOGR("./extents/kampala/urbFootprint_t2_subset_pruned.shp")
class(t2003)
proj4string(t2003)

#urban extent at 2008
t2008 <- readOGR("./extents/kampala/kampala2008.shp")
class(t2008)
proj4string(t2008)

#urban extent at 2015
t2015 <- readOGR("./extents/kampala/urbFootprint_t3_subset_pruned.shp")
class(t2015)
proj4string(t2015)


#pre_1963
test <- over(nodes, t1963)
length(test)
dim(nodes)
head(test)
nodes$pre_1963 <- 0
nodes$pre_1963[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1963)

#bw1963to1988  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t1988)
dim(test)
dim(nodes)
head(test)
nodes$bw1963to1988 <- 0
nodes$bw1963to1988[is.na(test$fid)==F & nodes$pre_1963 ==0] <- 1
table(nodes$pre_1963, nodes$bw1963to1988)

#bw1988to1996
test <- over(nodes, t1996)
dim(test)
dim(nodes)
head(test)
nodes$bw1988to1996 <- 0
nodes$bw1988to1996[is.na(test$id)==F & nodes$pre_1963 ==0 & nodes$bw1963to1988 ==0] <- 1
table(nodes$bw1963to1988, nodes$bw1988to1996)


#bw1996to2003  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2003)
dim(test)
dim(nodes)
head(test)
nodes$bw1996to2003 <- 0
nodes$bw1996to2003[is.na(test$fid)==F & nodes$pre_1963 ==0 & nodes$bw1963to1988 ==0 & nodes$bw1988to1996 == 0] <- 1
table(nodes$bw1988to1996, nodes$bw1996to2003)

#bw2003to2008
test <- over(nodes, t2008)
dim(test)
dim(nodes)
head(test)
nodes$bw2003to2008 <- 0
nodes$bw2003to2008[is.na(test$id)==F & nodes$pre_1963 ==0 & nodes$bw1963to1988 ==0 & nodes$bw1988to1996 == 0 & nodes$bw1996to2003 ==0] <- 1
table(nodes$bw1996to2003, nodes$bw2003to2008)


#bw2008to2015  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(nodes, t2015)
dim(test)
dim(nodes)
head(test)
nodes$bw2008to2015 <- 0
nodes$bw2008to2015[is.na(test$fid)==F & nodes$pre_1963 ==0 & nodes$bw1963to1988 ==0 & nodes$bw1988to1996 == 0 & nodes$bw1996to2003 ==0 & nodes$bw2003to2008 == 0] <- 1
table(nodes$bw2003to2008, nodes$bw2008to2015)


#bw2015to2020
nodes$bw2015to2020 <- 0
nodes$bw2015to2020[ nodes$pre_1963 ==0 & nodes$bw1963to1988 ==0 & nodes$bw1988to1996 == 0 & nodes$bw1996to2003 ==0 & nodes$bw2003to2008 == 0 & nodes$bw2008to2015 ==0] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1963 <- NA
tdat$bw1963to1988 <- NA
tdat$bw1988to1996 <- NA
tdat$bw1996to2003 <- NA
tdat$bw2003to2008 <- NA
tdat$bw2008to2015 <- NA
tdat$bw2015to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1963[i] <- mean(nodes@data$pre_1963[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1963to1988[i] <- mean(nodes@data$bw1963to1988[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1988to1996[i] <- mean(nodes@data$bw1988to1996[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1996to2003[i] <- mean(nodes@data$bw1996to2003[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2003to2008[i] <- mean(nodes@data$bw2003to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2015[i] <- mean(nodes@data$bw2008to2015[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2015to2020[i] <- mean(nodes@data$bw2015to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1890 #founding of fort in what would become old kampala
tdat$average_age <- ((historical_anchor + ((1963 - historical_anchor) / 2)) * tdat$pre_1963 ) + 
((1963 + ((1988 - 1963) / 2)) * tdat$bw1963to1988 ) + 
((1988 + ((1996 - 1988) / 2)) * tdat$bw1988to1996 ) + 
((1996 + ((2003 - 1996) / 2)) * tdat$bw1996to2003 ) + 
((2003 + ((2008 - 2003) / 2)) * tdat$bw2003to2008 ) + 
((2008 + ((2015 - 2008) / 2)) * tdat$bw2008to2015 ) + 
((2015 + ((2020 - 2015) / 2)) * tdat$bw2015to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="kampala-4427",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)



#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1963
test <- over(boulevards, t1963, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1963 <- 0
boulevards$pre_1963[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1963)

#bw1963to1988  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t1988, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1963to1988 <- 0
boulevards$bw1963to1988[is.na(test$fid)==F & boulevards$pre_1963 ==0] <- 1
table(boulevards$pre_1963, boulevards$bw1963to1988)

#bw1988to1996
test <- over(boulevards, t1996, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1988to1996 <- 0
boulevards$bw1988to1996[is.na(test$id)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1988 ==0] <- 1
table(boulevards$bw1963to1988, boulevards$bw1988to1996)


#bw1996to2003  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2003, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1996to2003 <- 0
boulevards$bw1996to2003[is.na(test$fid)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1988 ==0 & boulevards$bw1988to1996 == 0] <- 1
table(boulevards$bw1988to1996, boulevards$bw1996to2003)

#bw2003to2008
test <- over(boulevards, t2008, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2003to2008 <- 0
boulevards$bw2003to2008[is.na(test$id)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1988 ==0 & boulevards$bw1988to1996 == 0 & boulevards$bw1996to2003 ==0] <- 1
table(boulevards$bw1996to2003, boulevards$bw2003to2008)


#bw2008to2015  #NOTE: FORMAT IS DIFFERENT FOR THE LINCOLN ONES 
test <- over(boulevards, t2015, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2008to2015 <- 0
boulevards$bw2008to2015[is.na(test$fid)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1988 ==0 & boulevards$bw1988to1996 == 0 & boulevards$bw1996to2003 ==0 & boulevards$bw2003to2008 == 0] <- 1
table(boulevards$bw2003to2008, boulevards$bw2008to2015)


#bw2015to2020
boulevards$bw2015to2020 <- 0
boulevards$bw2015to2020[ boulevards$pre_1963 ==0 & boulevards$bw1963to1988 ==0 & boulevards$bw1988to1996 == 0 & boulevards$bw1996to2003 ==0 & boulevards$bw2003to2008 == 0 & boulevards$bw2008to2015 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[16], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "kampala-4427", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 16: KARA ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/togo-TGO_gpkg/kara-2026.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/togo-TGO_gpkg/kara-2026.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "kara-2026", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "kara-2026", ".Rdata", sep="")	)


#urban extent at 1985
t1985 <- readOGR("./extents/kara/kara1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1991
t1991 <- readOGR("./extents/kara/kara1991.shp")
class(t1991)
proj4string(t1991)

#urban extent at 2011
t2011 <- readOGR("./extents/kara/kara2011.shp")
class(t2011)
proj4string(t2011)

#urban extent at 2017
t2017 <- readOGR("./extents/kara/kara2017.shp")
class(t2017)
proj4string(t2017)



#pre_1985
test <- over(nodes, t1985)
length(test)
dim(nodes)
head(test)
nodes$pre_1985 <- 0
nodes$pre_1985[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1985)

#bw1985to1991
test <- over(nodes, t1991)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1991 <- 0
nodes$bw1985to1991[is.na(test$id)==F & nodes$pre_1985 ==0] <- 1
table(nodes$pre_1985, nodes$bw1985to1991)

#bw1991to2011
test <- over(nodes, t2011)
dim(test)
dim(nodes)
head(test)
nodes$bw1991to2011 <- 0
nodes$bw1991to2011[is.na(test$id)==F & nodes$pre_1985 ==0 & nodes$bw1985to1991 ==0] <- 1
table(nodes$bw1985to1991, nodes$bw1991to2011)

#bw2011to2017
test <- over(nodes, t2017)
dim(test)
dim(nodes)
head(test)
nodes$bw2011to2017 <- 0
nodes$bw2011to2017[is.na(test$id)==F & nodes$pre_1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2011 ==0] <- 1
table(nodes$bw1991to2011, nodes$bw2011to2017)

#bw2017to2020
nodes$bw2017to2020 <- 0
nodes$bw2017to2020[ nodes$pre_1985 ==0 & nodes$bw1985to1991 ==0 & nodes$bw1991to2011 ==0 & nodes$bw2011to2017 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1985 <- NA
tdat$bw1985to1991 <- NA
tdat$bw1991to2011 <- NA
tdat$bw2011to2017 <- NA
tdat$bw2017to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1985[i] <- mean(nodes@data$pre_1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1991[i] <- mean(nodes@data$bw1985to1991[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1991to2011[i] <- mean(nodes@data$bw1991to2011[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2011to2017[i] <- mean(nodes@data$bw2011to2017[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2017to2020[i] <- mean(nodes@data$bw2017to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1902 #wikipedia dates founding to 1902 consturction of a bridge over the river by the germans
tdat$average_age <- ((historical_anchor + ((1985 - historical_anchor) / 2)) * tdat$pre_1985 ) + 
((1985 + ((1991 - 1985) / 2)) * tdat$bw1985to1991 ) + 
((1991 + ((2011 - 1991) / 2)) * tdat$bw1991to2011 ) + 
((2011 + ((2017 - 2011) / 2)) * tdat$bw2011to2017 ) + 
((2017 + ((2020 - 2017) / 2)) * tdat$bw2017to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="kara-2026",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)


#pre_1985
test <- over(boulevards, t1985, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1985 <- 0
boulevards$pre_1985[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1985)

#bw1985to1991
test <- over(boulevards, t1991, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1991 <- 0
boulevards$bw1985to1991[is.na(test$id)==F & boulevards$pre_1985 ==0] <- 1
table(boulevards$pre_1985, boulevards$bw1985to1991)

#bw1991to2011
test <- over(boulevards, t2011, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1991to2011 <- 0
boulevards$bw1991to2011[is.na(test$id)==F & boulevards$pre_1985 ==0 & boulevards$bw1985to1991 ==0] <- 1
table(boulevards$bw1985to1991, boulevards$bw1991to2011)

#bw2011to2017
test <- over(boulevards, t2017, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2011to2017 <- 0
boulevards$bw2011to2017[is.na(test$id)==F & boulevards$pre_1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2011 ==0] <- 1
table(boulevards$bw1991to2011, boulevards$bw2011to2017)

#bw2017to2020
boulevards$bw2017to2020 <- 0
boulevards$bw2017to2020[ boulevards$pre_1985 ==0 & boulevards$bw1985to1991 ==0 & boulevards$bw1991to2011 ==0 & boulevards$bw2011to2017 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[17], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km




save(tdat, file=paste("./boeing_data/tcommunities/", "kara-2026", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 17: KHARTOUM ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/sudan-SDN_gpkg/khartoum-4335.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/sudan-SDN_gpkg/khartoum-4335.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "khartoum-4335", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "khartoum-4335", ".Rdata", sep="")	)


#urban extent at 1883
t1883 <- readOGR("./extents/khartoum/khartoum1883.shp")
class(t1883)
proj4string(t1883)

#urban extent at 1906
t1906 <- readOGR("./extents/khartoum/khartoum1906.shp")
class(t1906)
proj4string(t1906)

#urban extent at 1946
t1946 <- readOGR("./extents/khartoum/khartoum1946.shp")
class(t1946)
proj4string(t1946)

#urban extent at 1956
t1956 <- readOGR("./extents/khartoum/GreaterKhartoum1956.shp")
class(t1956)
proj4string(t1956)
t1956 <- spTransform(t1956, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 1965
t1965 <- readOGR("./extents/khartoum/khartoum1965.shp")
class(t1965)
proj4string(t1965)

#urban extent at 1988
t1988 <- readOGR("./extents/khartoum/urbFootprint_t1_subset_pruned.shp")
class(t1988)
proj4string(t1988)

#urban extent at 2000
t2000 <- readOGR("./extents/khartoum/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2009
t2009 <- readOGR("./extents/khartoum/khartoum2009.shp")
class(t2009)
proj4string(t2009)

#urban extent at 2014
t2014 <- readOGR("./extents/khartoum/urbFootprint_t3_subset_pruned.shp")
class(t2014)
proj4string(t2014)

#pre_1883
test <- over(nodes, t1883)
length(test)
dim(nodes)
head(test)
nodes$pre_1883 <- 0
nodes$pre_1883[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1883)

#bw1883to1906
test <- over(nodes, t1906)
dim(test)
dim(nodes)
head(test)
nodes$bw1883to1906 <- 0
nodes$bw1883to1906[is.na(test$id)==F & nodes$pre_1883 ==0] <- 1
table(nodes$pre_1883, nodes$bw1883to1906)

#bw1906to1946
test <- over(nodes, t1946)
dim(test)
dim(nodes)
head(test)
nodes$bw1906to1946 <- 0
nodes$bw1906to1946[is.na(test$id)==F & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0] <- 1
table(nodes$bw1883to1906, nodes$bw1906to1946)

#bw1946to1956
test <- over(nodes, t1956)
dim(test)
dim(nodes)
head(test)
nodes$bw1946to1956 <- 0
nodes$bw1946to1956[is.na(test$id)==F  & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0] <- 1
table(nodes$bw1906to1946, nodes$bw1946to1956)

#bw1956to1965
test <- over(nodes, t1965)
dim(test)
dim(nodes)
head(test)
nodes$bw1956to1965 <- 0
nodes$bw1956to1965[is.na(test$id)==F & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0 & nodes$bw1946to1956 ==0] <- 1
table(nodes$bw1946to1956, nodes$bw1956to1965)

#bw1965to1988
test <- over(nodes, t1988)
dim(test)
dim(nodes)
head(test)
nodes$bw1965to1988 <- 0
nodes$bw1965to1988[is.na(test$fid)==F & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0 & nodes$bw1946to1956 ==0 & nodes$bw1956to1965 ==0] <- 1
table(nodes$bw1956to1965, nodes$bw1965to1988)

#bw1988to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1988to2000 <- 0
nodes$bw1988to2000[is.na(test$fid)==F  & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0 & nodes$bw1946to1956 ==0 & nodes$bw1956to1965 ==0 & nodes$bw1965to1988 ==0] <- 1
table(nodes$bw1965to1988, nodes$bw1988to2000)

#bw2000to2009
test <- over(nodes, t2009)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2009 <- 0
nodes$bw2000to2009[is.na(test$id)==F & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0 & nodes$bw1946to1956 ==0 & nodes$bw1956to1965 ==0 & nodes$bw1965to1988 ==0 & nodes$bw1988to2000 ==0] <- 1
table(nodes$bw1988to2000, nodes$bw2000to2009)

#bw2009to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2009to2014 <- 0
nodes$bw2009to2014[is.na(test$fid)==F & nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0 & nodes$bw1946to1956 ==0 & nodes$bw1956to1965 ==0 & nodes$bw1965to1988 ==0 & nodes$bw1988to2000 ==0 & nodes$bw2000to2009 ==0] <- 1
table(nodes$bw2000to2009, nodes$bw2009to2014)


#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1883 ==0 & nodes$bw1883to1906 ==0 & nodes$bw1906to1946 ==0 & nodes$bw1946to1956 ==0 & nodes$bw1956to1965 ==0 & nodes$bw1965to1988 ==0 & nodes$bw1988to2000 ==0 & nodes$bw2000to2009 ==0 & nodes$bw2009to2014 ==0] <- 1

### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1883 <- NA
tdat$bw1883to1906 <- NA
tdat$bw1906to1946 <- NA
tdat$bw1946to1956 <- NA
tdat$bw1956to1965 <- NA
tdat$bw1965to1988 <- NA
tdat$bw1988to2000 <- NA
tdat$bw2000to2009 <- NA
tdat$bw2009to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1883[i] <- mean(nodes@data$pre_1883[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1883to1906[i] <- mean(nodes@data$bw1883to1906[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1906to1946[i] <- mean(nodes@data$bw1906to1946[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1946to1956[i] <- mean(nodes@data$bw1946to1956[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1956to1965[i] <- mean(nodes@data$bw1956to1965[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1965to1988[i] <- mean(nodes@data$bw1965to1988[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1988to2000[i] <- mean(nodes@data$bw1988to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2009[i] <- mean(nodes@data$bw2000to2009[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2009to2014[i] <- mean(nodes@data$bw2009to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1821 
tdat$average_age <- ((historical_anchor + ((1883 - historical_anchor) / 2)) * tdat$pre_1883 ) + 
((1883 + ((1906 - 1883) / 2)) * tdat$bw1883to1906 ) + 
((1906 + ((1946 - 1906) / 2)) * tdat$bw1906to1946 ) + 
((1946 + ((1956 - 1946) / 2)) * tdat$bw1946to1956 ) + 
((1956 + ((1965 - 1956) / 2)) * tdat$bw1956to1965 ) + 
((1965 + ((1988 - 1965) / 2)) * tdat$bw1965to1988 ) + 
((1988 + ((2000 - 1988) / 2)) * tdat$bw1988to2000 ) + 
((2000 + ((2009 - 2000) / 2)) * tdat$bw2000to2009 )  +
((2009 + ((2014 - 2009) / 2)) * tdat$bw2009to2014 )  +
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="khartoum-4335",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)



#pre_1883
test <- over(boulevards, t1883, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1883 <- 0
boulevards$pre_1883[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1883)

#bw1883to1906
test <- over(boulevards, t1906, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1883to1906 <- 0
boulevards$bw1883to1906[is.na(test$id)==F & boulevards$pre_1883 ==0] <- 1
table(boulevards$pre_1883, boulevards$bw1883to1906)

#bw1906to1946
test <- over(boulevards, t1946, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1906to1946 <- 0
boulevards$bw1906to1946[is.na(test$id)==F & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0] <- 1
table(boulevards$bw1883to1906, boulevards$bw1906to1946)

#bw1946to1956
test <- over(boulevards, t1956, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1946to1956 <- 0
boulevards$bw1946to1956[is.na(test$id)==F  & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0] <- 1
table(boulevards$bw1906to1946, boulevards$bw1946to1956)

#bw1956to1965
test <- over(boulevards, t1965, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1956to1965 <- 0
boulevards$bw1956to1965[is.na(test$id)==F & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0 & boulevards$bw1946to1956 ==0] <- 1
table(boulevards$bw1946to1956, boulevards$bw1956to1965)

#bw1965to1988
test <- over(boulevards, t1988, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1965to1988 <- 0
boulevards$bw1965to1988[is.na(test$fid)==F & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0 & boulevards$bw1946to1956 ==0 & boulevards$bw1956to1965 ==0] <- 1
table(boulevards$bw1956to1965, boulevards$bw1965to1988)

#bw1988to2000
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1988to2000 <- 0
boulevards$bw1988to2000[is.na(test$fid)==F  & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0 & boulevards$bw1946to1956 ==0 & boulevards$bw1956to1965 ==0 & boulevards$bw1965to1988 ==0] <- 1
table(boulevards$bw1965to1988, boulevards$bw1988to2000)

#bw2000to2009
test <- over(boulevards, t2009, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2009 <- 0
boulevards$bw2000to2009[is.na(test$id)==F & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0 & boulevards$bw1946to1956 ==0 & boulevards$bw1956to1965 ==0 & boulevards$bw1965to1988 ==0 & boulevards$bw1988to2000 ==0] <- 1
table(boulevards$bw1988to2000, boulevards$bw2000to2009)

#bw2009to2014
test <- over(boulevards, t2014, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2009to2014 <- 0
boulevards$bw2009to2014[is.na(test$fid)==F & boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0 & boulevards$bw1946to1956 ==0 & boulevards$bw1956to1965 ==0 & boulevards$bw1965to1988 ==0 & boulevards$bw1988to2000 ==0 & boulevards$bw2000to2009 ==0] <- 1
table(boulevards$bw2000to2009, boulevards$bw2009to2014)


#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1883 ==0 & boulevards$bw1883to1906 ==0 & boulevards$bw1906to1946 ==0 & boulevards$bw1946to1956 ==0 & boulevards$bw1956to1965 ==0 & boulevards$bw1965to1988 ==0 & boulevards$bw1988to2000 ==0 & boulevards$bw2000to2009 ==0 & boulevards$bw2009to2014 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[18], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "khartoum-4335", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 18: KIGALI ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/rwanda-RWA_gpkg/kigali-4172.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/rwanda-RWA_gpkg/kigali-4172.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "kigali-4172", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "kigali-4172", ".Rdata", sep="")	)


#urban extent at 1967
t1967 <- readOGR("./extents/kigali/Kigali 1967 LOC1 .shp")
class(t1967)
proj4string(t1967)
t1967@data <- as.data.frame(matrix(1, nrow=nrow(t1967), ncol=1))
colnames(t1967@data) <- c("id")

#urban extent at 1981
t1981 <- readOGR("./extents/kigali/Kigali 1981 LOC1.shp")
class(t1981)
proj4string(t1981)
t1981@data <- as.data.frame(matrix(1, nrow=nrow(t1981), ncol=1))
colnames(t1981@data) <- c("id")

#urban extent at 1987
t1987 <- readOGR("./extents/kigali/urbFootprint_t1_subset_pruned.shp")
class(t1987)
proj4string(t1987)

#urban extent at 1994
t1994 <- readOGR("./extents/kigali/kigali1994.shp")
class(t1994)
proj4string(t1994)

#urban extent at 1999
t1999 <- readOGR("./extents/kigali/urbFootprint_t2_subset_pruned.shp")
class(t1999)
proj4string(t1999)

#urban extent at 2008
t2008 <- readOGR("./extents/kigali/kigali2008.shp")
class(t2008)
proj4string(t2008)

#urban extent at 2014
t2014 <- readOGR("./extents/kigali/urbFootprint_t3_subset_pruned.shp")
class(t2014)
proj4string(t2014)

#pre_1967
test <- over(nodes, t1967)
length(test)
dim(nodes)
head(test)
nodes$pre_1967 <- 0
nodes$pre_1967[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1967)

#bw1967to1981
test <- over(nodes, t1981)
dim(test)
dim(nodes)
head(test)
nodes$bw1967to1981 <- 0
nodes$bw1967to1981[is.na(test$id)==F & nodes$pre_1967 ==0] <- 1
table(nodes$pre_1967, nodes$bw1967to1981)

#bw1981to1987
test <- over(nodes, t1987)
dim(test)
dim(nodes)
head(test)
nodes$bw1981to1987 <- 0
nodes$bw1981to1987[is.na(test$fid)==F & nodes$pre_1967 ==0 & nodes$bw1967to1981 ==0] <- 1
table(nodes$bw1967to1981, nodes$bw1981to1987)

#bw1987to1994
test <- over(nodes, t1994)
dim(test)
dim(nodes)
head(test)
nodes$bw1987to1994 <- 0
nodes$bw1987to1994[is.na(test$id)==F & nodes$pre_1967 ==0 & nodes$bw1967to1981 ==0 & nodes$bw1981to1987 ==0] <- 1
table(nodes$bw1981to1987, nodes$bw1987to1994)


#bw1994to1999
test <- over(nodes, t1999)
dim(test)
dim(nodes)
head(test)
nodes$bw1994to1999 <- 0
nodes$bw1994to1999[is.na(test$fid)==F & nodes$pre_1967 ==0 & nodes$bw1967to1981 ==0 & nodes$bw1981to1987 ==0 & nodes$bw1987to1994 ==0] <- 1
table(nodes$bw1987to1994, nodes$bw1994to1999)

#bw1999to2008
test <- over(nodes, t2008)
dim(test)
dim(nodes)
head(test)
nodes$bw1999to2008 <- 0
nodes$bw1999to2008[is.na(test$id)==F & nodes$pre_1967 ==0 & nodes$bw1967to1981 ==0 & nodes$bw1981to1987 ==0 & nodes$bw1987to1994 ==0 & nodes$bw1994to1999 ==0] <- 1
table(nodes$bw1994to1999, nodes$bw1999to2008)

#bw2008to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2008to2014 <- 0
nodes$bw2008to2014[is.na(test$fid)==F & nodes$pre_1967 ==0 & nodes$bw1967to1981 ==0 & nodes$bw1981to1987 ==0 & nodes$bw1987to1994 ==0 & nodes$bw1994to1999 ==0 & nodes$bw1999to2008 ==0] <- 1
table(nodes$bw1999to2008, nodes$bw2008to2014)

#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1967 ==0 & nodes$bw1967to1981 ==0 & nodes$bw1981to1987 ==0 & nodes$bw1987to1994 ==0 & nodes$bw1994to1999 ==0 & nodes$bw1999to2008 ==0 & nodes$bw2008to2014 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1967 <- NA
tdat$bw1967to1981 <- NA
tdat$bw1981to1987 <- NA
tdat$bw1987to1994 <- NA
tdat$bw1994to1999 <- NA
tdat$bw1999to2008 <- NA
tdat$bw2008to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1967[i] <- mean(nodes@data$pre_1967[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1967to1981[i] <- mean(nodes@data$bw1967to1981[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1981to1987[i] <- mean(nodes@data$bw1981to1987[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1987to1994[i] <- mean(nodes@data$bw1987to1994[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1994to1999[i] <- mean(nodes@data$bw1994to1999[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1999to2008[i] <- mean(nodes@data$bw1999to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2014[i] <- mean(nodes@data$bw2008to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1907 #see: wikipedia
tdat$average_age <- ((historical_anchor + ((1967 - historical_anchor) / 2)) * tdat$pre_1967 ) + 
((1967 + ((1981 - 1967) / 2)) * tdat$bw1967to1981 ) + 
((1981 + ((1987 - 1981) / 2)) * tdat$bw1981to1987 ) + 
((1987 + ((1994 - 1987) / 2)) * tdat$bw1987to1994 ) + 
((1994 + ((1999 - 1994) / 2)) * tdat$bw1994to1999 ) + 
((1999 + ((2008 - 1999) / 2)) * tdat$bw1999to2008 ) + 
((2008 + ((2014 - 2008) / 2)) * tdat$bw2008to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="kigali-4172",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1967==1 | nodes$bw1967to1981 ==1 | nodes$bw1981to1987 ==1  ] <- dist1[nodes$pre_1967==1 | nodes$bw1967to1981 ==1 | nodes$bw1981to1987 ==1 , 1]
nodes$dist_to_hq[nodes$bw1994to1999 ==1 | nodes$bw1999to2008 ==1 | nodes$bw2008to2014 ==1 | nodes$bw2014to2020 ==1  ] <- dist2[nodes$bw1994to1999 ==1 | nodes$bw1999to2008 ==1 | nodes$bw2008to2014 ==1 | nodes$bw2014to2020 ==1 , 1]
nodes$dist_to_hq[nodes$bw1987to1994 ==1] <- (dist1[nodes$bw1987to1994 ==1,1] * (4/6)) + (dist2[nodes$bw1987to1994 ==1,1] * (2/6)) #just the time period that is crossed the swithceroo
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['secondary', 'trunk_link']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1967
test <- over(boulevards, t1967, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1967 <- 0
boulevards$pre_1967[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1967)

#bw1967to1981
test <- over(boulevards, t1981, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1967to1981 <- 0
boulevards$bw1967to1981[is.na(test$id)==F & boulevards$pre_1967 ==0] <- 1
table(boulevards$pre_1967, boulevards$bw1967to1981)

#bw1981to1987
test <- over(boulevards, t1987, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1981to1987 <- 0
boulevards$bw1981to1987[is.na(test$fid)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1981 ==0] <- 1
table(boulevards$bw1967to1981, boulevards$bw1981to1987)

#bw1987to1994
test <- over(boulevards, t1994, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1987to1994 <- 0
boulevards$bw1987to1994[is.na(test$id)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1981 ==0 & boulevards$bw1981to1987 ==0] <- 1
table(boulevards$bw1981to1987, boulevards$bw1987to1994)


#bw1994to1999
test <- over(boulevards, t1999, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1994to1999 <- 0
boulevards$bw1994to1999[is.na(test$fid)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1981 ==0 & boulevards$bw1981to1987 ==0 & boulevards$bw1987to1994 ==0] <- 1
table(boulevards$bw1987to1994, boulevards$bw1994to1999)

#bw1999to2008
test <- over(boulevards, t2008, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1999to2008 <- 0
boulevards$bw1999to2008[is.na(test$id)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1981 ==0 & boulevards$bw1981to1987 ==0 & boulevards$bw1987to1994 ==0 & boulevards$bw1994to1999 ==0] <- 1
table(boulevards$bw1994to1999, boulevards$bw1999to2008)

#bw2008to2014
test <- over(boulevards, t2014, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2008to2014 <- 0
boulevards$bw2008to2014[is.na(test$fid)==F & boulevards$pre_1967 ==0 & boulevards$bw1967to1981 ==0 & boulevards$bw1981to1987 ==0 & boulevards$bw1987to1994 ==0 & boulevards$bw1994to1999 ==0 & boulevards$bw1999to2008 ==0] <- 1
table(boulevards$bw1999to2008, boulevards$bw2008to2014)

#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1967 ==0 & boulevards$bw1967to1981 ==0 & boulevards$bw1981to1987 ==0 & boulevards$bw1987to1994 ==0 & boulevards$bw1994to1999 ==0 & boulevards$bw1999to2008 ==0 & boulevards$bw2008to2014 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[19], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "kigali-4172", "_tdat_wyears_NEW", ".Rdata", sep="")	)




#####################################
##### CITY 19: KINSHASA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/democratic_republic_of_the_congo-COD_gpkg/kinshasa-3209.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/democratic_republic_of_the_congo-COD_gpkg/kinshasa-3209.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "kinshasa-3209", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "kinshasa-3209", ".Rdata", sep="")	)


#urban extent at 1930
t1930 <- readOGR("./historical_maps_todigitize/kinshasa/Kinshasa 1930 Merged.shp")
class(t1930)
proj4string(t1930)
t1930@data <- as.data.frame(matrix(1, nrow=nrow(t1930), ncol=1))
colnames(t1930@data) <- c("id")

#urban extent at 1949
t1949 <- readOGR("./extents/kinshasa/Kinshasa1949.shp")
class(t1949)
proj4string(t1949)

#urban extent at 1967
t1967 <- readOGR("./extents/kinshasa/kinshasa1967.shp")
class(t1967)
proj4string(t1967)

#urban extent at 1975
t1975 <- readOGR("./extents/kinshasa/kinshasa1975.shp")
class(t1975)
proj4string(t1975)

#urban extent at 1994
t1994 <- readOGR("./extents/kinshasa/urbFootprint_t1_subset_pruned.shp")
class(t1994)
proj4string(t1994)

#urban extent at 2000
t2000 <- readOGR("./extents/kinshasa/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2013
t2013 <- readOGR("./extents/kinshasa/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)



#pre_1930
test <- over(nodes, t1930)
length(test)
dim(nodes)
head(test)
nodes$pre_1930 <- 0
nodes$pre_1930[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1930)


#bw1930to1949
test <- over(nodes, t1949)
dim(test)
dim(nodes)
head(test)
nodes$bw1930to1949 <- 0
nodes$bw1930to1949[is.na(test$id)==F & nodes$pre_1930 ==0] <- 1
table(nodes$pre_1930, nodes$bw1930to1949)


#bw1949to1967
test <- over(nodes, t1967)
dim(test)
dim(nodes)
head(test)
nodes$bw1949to1967 <- 0
nodes$bw1949to1967[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1949 ==0] <- 1
table(nodes$bw1930to1949, nodes$bw1949to1967)


#bw1967to1975
test <- over(nodes, t1975)
dim(test)
dim(nodes)
head(test)
nodes$bw1967to1975 <- 0
nodes$bw1967to1975[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1949 ==0 & nodes$bw1949to1967 ==0] <- 1
table(nodes$bw1949to1967, nodes$bw1967to1975)


#bw1975to1994
test <- over(nodes, t1994)
dim(test)
dim(nodes)
head(test)
nodes$bw1975to1994 <- 0
nodes$bw1975to1994[is.na(test$fid)==F & nodes$pre_1930 ==0 & nodes$bw1930to1949 ==0 & nodes$bw1949to1967 ==0 & nodes$bw1967to1975 ==0] <- 1
table(nodes$bw1967to1975, nodes$bw1975to1994)

#bw1994to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1994to2000 <- 0
nodes$bw1994to2000[is.na(test$fid)==F & nodes$pre_1930 ==0 & nodes$bw1930to1949 ==0 & nodes$bw1949to1967 ==0 & nodes$bw1967to1975 ==0 & nodes$bw1975to1994 ==0] <- 1
table(nodes$bw1975to1994, nodes$bw1994to2000)


#bw2000to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2013 <- 0
nodes$bw2000to2013[is.na(test$fid)==F  & nodes$pre_1930 ==0 & nodes$bw1930to1949 ==0 & nodes$bw1949to1967 ==0 & nodes$bw1967to1975 ==0 & nodes$bw1975to1994 ==0 & nodes$bw1994to2000 ==0] <- 1
table(nodes$bw1994to2000, nodes$bw2000to2013)


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1930 ==0 & nodes$bw1930to1949 ==0 & nodes$bw1949to1967 ==0 & nodes$bw1967to1975 ==0 & nodes$bw1975to1994 ==0 & nodes$bw1994to2000 ==0 & nodes$bw2000to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1930 <- NA
tdat$bw1930to1949 <- NA
tdat$bw1949to1967 <- NA
tdat$bw1967to1975 <- NA
tdat$bw1975to1994 <- NA
tdat$bw1994to2000 <- NA
tdat$bw2000to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1930[i] <- mean(nodes@data$pre_1930[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1930to1949[i] <- mean(nodes@data$bw1930to1949[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1949to1967[i] <- mean(nodes@data$bw1949to1967[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1967to1975[i] <- mean(nodes@data$bw1967to1975[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1975to1994[i] <- mean(nodes@data$bw1975to1994[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1994to2000[i] <- mean(nodes@data$bw1994to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2013[i] <- mean(nodes@data$bw2000to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1881 #date of founding as trading post
tdat$average_age <- ((historical_anchor + ((1930 - historical_anchor) / 2)) * tdat$pre_1930 ) + 
((1930 + ((1949 - 1930) / 2)) * tdat$bw1930to1949 ) + 
((1949 + ((1967 - 1949) / 2)) * tdat$bw1949to1967 ) + 
((1967 + ((1975 - 1967) / 2)) * tdat$bw1967to1975 ) + 
((1975 + ((1994 - 1975) / 2)) * tdat$bw1975to1994 ) + 
((1994 + ((2000 - 1994) / 2)) * tdat$bw1994to2000 ) + 
((2000 + ((2013 - 2000) / 2)) * tdat$bw2000to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="kinshasa-3209",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts3 <- SpatialPoints(caps[,c("cap_x3","cap_y3")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts3.aedc <- spTransform(caps.pts3, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)
dist3 <- gDistance(caps.pts3.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1930==1 | nodes$bw1930to1949 ==1 | nodes$bw1949to1967 ==1 |nodes$bw1967to1975 ==1 ] <- dist1[nodes$pre_1930==1 | nodes$bw1930to1949 ==1 | nodes$bw1949to1967 ==1 |nodes$bw1967to1975 ==1 ,1] 
nodes$dist_to_hq[nodes$bw1994to2000 ==1  ] <- dist2[nodes$bw1994to2000 ==1 ,1] 
nodes$dist_to_hq[nodes$bw2000to2013 ==1 | nodes$bw2013to2020 ==1  ] <- dist3[nodes$bw2000to2013 ==1 | nodes$bw2013to2020 ==1,1] 
nodes$dist_to_hq[nodes$bw1975to1994 ==1] <- (dist1[nodes$bw1975to1994 ==1,1] * (3/29)) + (dist2[nodes$bw1975to1994 ==1,1] * (26/29)) #just the time period that is crossed the swithceroo
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)



#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1930
test <- over(boulevards, t1930, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1930 <- 0
boulevards$pre_1930[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1930)


#bw1930to1949
test <- over(boulevards, t1949, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1930to1949 <- 0
boulevards$bw1930to1949[is.na(test$id)==F & boulevards$pre_1930 ==0] <- 1
table(boulevards$pre_1930, boulevards$bw1930to1949)


#bw1949to1967
test <- over(boulevards, t1967, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1949to1967 <- 0
boulevards$bw1949to1967[is.na(test$id)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1949 ==0] <- 1
table(boulevards$bw1930to1949, boulevards$bw1949to1967)


#bw1967to1975
test <- over(boulevards, t1975, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1967to1975 <- 0
boulevards$bw1967to1975[is.na(test$id)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1949 ==0 & boulevards$bw1949to1967 ==0] <- 1
table(boulevards$bw1949to1967, boulevards$bw1967to1975)


#bw1975to1994
test <- over(boulevards, t1994, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1975to1994 <- 0
boulevards$bw1975to1994[is.na(test$fid)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1949 ==0 & boulevards$bw1949to1967 ==0 & boulevards$bw1967to1975 ==0] <- 1
table(boulevards$bw1967to1975, boulevards$bw1975to1994)

#bw1994to2000
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1994to2000 <- 0
boulevards$bw1994to2000[is.na(test$fid)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1949 ==0 & boulevards$bw1949to1967 ==0 & boulevards$bw1967to1975 ==0 & boulevards$bw1975to1994 ==0] <- 1
table(boulevards$bw1975to1994, boulevards$bw1994to2000)


#bw2000to2013
test <- over(boulevards, t2013)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2013 <- 0
boulevards$bw2000to2013[is.na(test$fid)==F  & boulevards$pre_1930 ==0 & boulevards$bw1930to1949 ==0 & boulevards$bw1949to1967 ==0 & boulevards$bw1967to1975 ==0 & boulevards$bw1975to1994 ==0 & boulevards$bw1994to2000 ==0] <- 1
table(boulevards$bw1994to2000, boulevards$bw2000to2013)


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1930 ==0 & boulevards$bw1930to1949 ==0 & boulevards$bw1949to1967 ==0 & boulevards$bw1967to1975 ==0 & boulevards$bw1975to1994 ==0 & boulevards$bw1994to2000 ==0 & boulevards$bw2000to2013 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[20], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "kinshasa-3209", "_tdat_wyears_NEW", ".Rdata", sep="")	)




#####################################
##### CITY 20: LAGOS ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/lagos-2125.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/lagos-2125.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "lagos-2125", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "lagos-2125", ".Rdata", sep="")	)



#urban extent at 1931
t1931 <- readOGR("./extents/lagos/lagos1931.shp")
class(t1931)
proj4string(t1931)


#urban extent at 1960
t1960 <- readOGR("./extents/lagos/lagos1960.shp")
class(t1960)
proj4string(t1960)


#urban extent at 1978
t1978 <- readOGR("./extents/lagos/lagos1978.shp")
class(t1978)
proj4string(t1978)


#urban extent at 1984
t1984 <- readOGR("./extents/lagos/urbFootprint_t1_subset_pruned.shp")
class(t1984)
proj4string(t1984)


#urban extent at 1992
t1992 <- readOGR("./extents/lagos/lagos1992.shp")
class(t1992)
proj4string(t1992)


#urban extent at 2000
t2000 <- readOGR("./extents/lagos/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)


#urban extent at 2007
t2007 <- readOGR("./extents/lagos/lagos2007.shp")
class(t2007)
proj4string(t2007)


#urban extent at 2013
t2013 <- readOGR("./extents/lagos/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)


#pre_1931
test <- over(nodes, t1931)
length(test)
dim(nodes)
head(test)
nodes$pre_1931 <- 0
nodes$pre_1931[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1931)

#bw1931to1960
test <- over(nodes, t1960)
dim(test)
dim(nodes)
head(test)
nodes$bw1931to1960 <- 0
nodes$bw1931to1960[is.na(test$id)==F & nodes$pre_1931 ==0] <- 1
table(nodes$pre_1931, nodes$bw1931to1960)


#bw1960to1978
test <- over(nodes, t1978)
dim(test)
dim(nodes)
head(test)
nodes$bw1960to1978 <- 0
nodes$bw1960to1978[is.na(test$id)==F & nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0] <- 1
table(nodes$bw1931to1960, nodes$bw1960to1978)


#bw1978to1984
test <- over(nodes, t1984)
dim(test)
dim(nodes)
head(test)
nodes$bw1978to1984 <- 0
nodes$bw1978to1984[is.na(test$fid)==F & nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0 & nodes$bw1960to1978 ==0] <- 1
table(nodes$bw1960to1978, nodes$bw1978to1984)

#bw1984to1992
test <- over(nodes, t1992)
dim(test)
dim(nodes)
head(test)
nodes$bw1984to1992 <- 0
nodes$bw1984to1992[is.na(test$id)==F & nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0 & nodes$bw1960to1978 ==0 & nodes$bw1978to1984 ==0] <- 1
table(nodes$bw1978to1984, nodes$bw1984to1992)

#bw1992to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1992to2000 <- 0
nodes$bw1992to2000[is.na(test$fid)==F & nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0 & nodes$bw1960to1978 ==0 & nodes$bw1978to1984 ==0 & nodes$bw1984to1992 ==0] <- 1
table(nodes$bw1984to1992, nodes$bw1992to2000)


#bw2000to2007
test <- over(nodes, t2007)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2007 <- 0
nodes$bw2000to2007[is.na(test$id)==F & nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0 & nodes$bw1960to1978 ==0 & nodes$bw1978to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2000 ==0] <- 1
table(nodes$bw1992to2000, nodes$bw2000to2007)


#bw2007to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2007to2013 <- 0
nodes$bw2007to2013[is.na(test$fid)==F & nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0 & nodes$bw1960to1978 ==0 & nodes$bw1978to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2000 ==0 & nodes$bw2000to2007 ==0] <- 1
table(nodes$bw2000to2007, nodes$bw2007to2013)


#bw2010to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1931 ==0 & nodes$bw1931to1960 ==0 & nodes$bw1960to1978 ==0 & nodes$bw1978to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2000 ==0 & nodes$bw2000to2007 ==0 & nodes$bw2007to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1931 <- NA
tdat$bw1931to1960 <- NA
tdat$bw1960to1978 <- NA
tdat$bw1978to1984 <- NA
tdat$bw1984to1992 <- NA
tdat$bw1992to2000 <- NA
tdat$bw2000to2007 <- NA
tdat$bw2007to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1931[i] <- mean(nodes@data$pre_1931[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1931to1960[i] <- mean(nodes@data$bw1931to1960[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1960to1978[i] <- mean(nodes@data$bw1960to1978[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1978to1984[i] <- mean(nodes@data$bw1978to1984[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1984to1992[i] <- mean(nodes@data$bw1984to1992[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1992to2000[i] <- mean(nodes@data$bw1992to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2007[i] <- mean(nodes@data$bw2000to2007[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2007to2013[i] <- mean(nodes@data$bw2007to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1851 #onset of british control, founding of modern lagos
tdat$average_age <- ((historical_anchor + ((1931 - historical_anchor) / 2)) * tdat$pre_1931 ) + 
((1931 + ((1960 - 1931) / 2)) * tdat$bw1931to1960 ) + 
((1960 + ((1978 - 1960) / 2)) * tdat$bw1960to1978 ) + 
((1978 + ((1984 - 1978) / 2)) * tdat$bw1978to1984 ) + 
((1984 + ((1992 - 1984) / 2)) * tdat$bw1984to1992 ) + 
((1992 + ((2000 - 1992) / 2)) * tdat$bw1992to2000 ) + 
((2000 + ((2007 - 2000) / 2)) * tdat$bw2000to2007 ) + 
((2007 + ((2013 - 2007) / 2)) * tdat$bw2007to2013 )  + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="lagos-2125",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts3 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts3.aedc <- spTransform(caps.pts3, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))


nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)
dist3 <- gDistance(caps.pts3.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1931 ==1 | nodes$bw1931to1960 ==1 | nodes$bw1960to1978 ==1 | nodes$bw1978to1984 ==1  ] <- dist1[nodes$pre_1931 ==1 | nodes$bw1931to1960 ==1 | nodes$bw1960to1978 ==1 | nodes$bw1978to1984 ==1 ,1] 
nodes$dist_to_hq[nodes$bw1992to2000 ==1  ] <- dist2[nodes$bw1992to2000 ==1 ,1] 
nodes$dist_to_hq[nodes$bw2007to2013 ==1  | nodes$bw2013to2020 ==1] <- dist3[nodes$bw2007to2013 ==1  | nodes$bw2013to2020 ==1 ,1] 
nodes$dist_to_hq[nodes$bw1984to1992 ==1] <- (dist1[nodes$bw1984to1992 ==1,1] * (2/8)) + (dist2[nodes$bw1984to1992 ==1,1] * (6/8))
nodes$dist_to_hq[nodes$bw2000to2007 ==1] <- (dist2[nodes$bw2000to2007 ==1,1] * (6/7)) + (dist3[nodes$bw2000to2007 ==1,1] * (1/7))


table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['motorway_link', 'tertiary']", "['motorway', 'secondary']", "['secondary', 'motorway']", "['tertiary', 'motorway_link']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1931
test <- over(boulevards, t1931, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1931 <- 0
boulevards$pre_1931[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1931)

#bw1931to1960
test <- over(boulevards, t1960, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1931to1960 <- 0
boulevards$bw1931to1960[is.na(test$id)==F & boulevards$pre_1931 ==0] <- 1
table(boulevards$pre_1931, boulevards$bw1931to1960)


#bw1960to1978
test <- over(boulevards, t1978, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1960to1978 <- 0
boulevards$bw1960to1978[is.na(test$id)==F & boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0] <- 1
table(boulevards$bw1931to1960, boulevards$bw1960to1978)


#bw1978to1984
test <- over(boulevards, t1984, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1978to1984 <- 0
boulevards$bw1978to1984[is.na(test$fid)==F & boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0 & boulevards$bw1960to1978 ==0] <- 1
table(boulevards$bw1960to1978, boulevards$bw1978to1984)

#bw1984to1992
test <- over(boulevards, t1992, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1984to1992 <- 0
boulevards$bw1984to1992[is.na(test$id)==F & boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0 & boulevards$bw1960to1978 ==0 & boulevards$bw1978to1984 ==0] <- 1
table(boulevards$bw1978to1984, boulevards$bw1984to1992)

#bw1992to2000
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1992to2000 <- 0
boulevards$bw1992to2000[is.na(test$fid)==F & boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0 & boulevards$bw1960to1978 ==0 & boulevards$bw1978to1984 ==0 & boulevards$bw1984to1992 ==0] <- 1
table(boulevards$bw1984to1992, boulevards$bw1992to2000)


#bw2000to2007
test <- over(boulevards, t2007, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2007 <- 0
boulevards$bw2000to2007[is.na(test$id)==F & boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0 & boulevards$bw1960to1978 ==0 & boulevards$bw1978to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2000 ==0] <- 1
table(boulevards$bw1992to2000, boulevards$bw2000to2007)


#bw2007to2013
test <- over(boulevards, t2013, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2007to2013 <- 0
boulevards$bw2007to2013[is.na(test$fid)==F & boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0 & boulevards$bw1960to1978 ==0 & boulevards$bw1978to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2000 ==0 & boulevards$bw2000to2007 ==0] <- 1
table(boulevards$bw2000to2007, boulevards$bw2007to2013)


#bw2010to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1931 ==0 & boulevards$bw1931to1960 ==0 & boulevards$bw1960to1978 ==0 & boulevards$bw1978to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2000 ==0 & boulevards$bw2000to2007 ==0 & boulevards$bw2007to2013 ==0] <- 1




#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[21], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "lagos-2125", "_tdat_wyears_NEW", ".Rdata", sep="")	)

#####################################
##### CITY 21: LUANDA ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/angola-AGO_gpkg/luanda-3050.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/angola-AGO_gpkg/luanda-3050.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "luanda-3050", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "luanda-3050", ".Rdata", sep="")	)

#urban extent at 1963
t1963 <- readOGR("./extents/luanda/luanda1963.shp")
class(t1963)
proj4string(t1963)

#urban extent at 1978
t1978 <- readOGR("./extents/luanda/luanda1978.shp")
class(t1978)
proj4string(t1978)

#urban extent at 1991
t1991 <- readOGR("./extents/luanda/luanda1991.shp")
class(t1991)
proj4string(t1991)

#urban extent at 2000
t2000 <- readOGR("./extents/luanda/luanda2000.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2008
t2008 <- readOGR("./extents/luanda/luanda2008.shp")
class(t2008)
proj4string(t2008)

#urban extent at 2014
t2014 <- readOGR("./extents/luanda/luanda2014.shp")
class(t2014)
proj4string(t2014)



#pre_1963
test <- over(nodes, t1963)
length(test)
dim(nodes)
head(test)
nodes$pre_1963 <- 0
nodes$pre_1963[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1963)

#bw1963to1978
test <- over(nodes, t1978)
dim(test)
dim(nodes)
head(test)
nodes$bw1963to1978 <- 0
nodes$bw1963to1978[is.na(test$id)==F & nodes$pre_1963 ==0] <- 1
table(nodes$pre_1963, nodes$bw1963to1978)

#bw1978to1991
test <- over(nodes, t1991)
dim(test)
dim(nodes)
head(test)
nodes$bw1978to1991 <- 0
nodes$bw1978to1991[is.na(test$id)==F & nodes$pre_1963 ==0 & nodes$bw1963to1978 ==0] <- 1
table(nodes$bw1963to1978, nodes$bw1978to1991)


#bw1991to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1991to2000 <- 0
nodes$bw1991to2000[is.na(test$id)==F & nodes$pre_1963 ==0 & nodes$bw1963to1978 ==0 & nodes$bw1978to1991 ==0] <- 1
table(nodes$bw1978to1991, nodes$bw1991to2000)

#bw2000to2008
test <- over(nodes, t2008)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2008 <- 0
nodes$bw2000to2008[is.na(test$id)==F  & nodes$pre_1963 ==0 & nodes$bw1963to1978 ==0 & nodes$bw1978to1991 ==0 & nodes$bw1991to2000 ==0] <- 1
table(nodes$bw1991to2000, nodes$bw2000to2008)

#bw2008to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2008to2014 <- 0
nodes$bw2008to2014[is.na(test$id)==F & nodes$pre_1963 ==0 & nodes$bw1963to1978 ==0 & nodes$bw1978to1991 ==0 & nodes$bw1991to2000 ==0 & nodes$bw2000to2008 ==0] <- 1
table(nodes$bw2000to2008, nodes$bw2008to2014)


#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1963 ==0 & nodes$bw1963to1978 ==0 & nodes$bw1978to1991 ==0 & nodes$bw1991to2000 ==0 & nodes$bw2000to2008 ==0 & nodes$bw2008to2014 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1963 <- NA
tdat$bw1963to1978 <- NA
tdat$bw1978to1991 <- NA
tdat$bw1991to2000 <- NA
tdat$bw2000to2008 <- NA
tdat$bw2008to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1963[i] <- mean(nodes@data$pre_1963[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1963to1978[i] <- mean(nodes@data$bw1963to1978[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1978to1991[i] <- mean(nodes@data$bw1978to1991[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1991to2000[i] <- mean(nodes@data$bw1991to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2008[i] <- mean(nodes@data$bw2000to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2014[i] <- mean(nodes@data$bw2008to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1627 #see wikipedia entry
tdat$average_age <- ((historical_anchor + ((1963 - historical_anchor) / 2)) * tdat$pre_1963 ) + 
((1963 + ((1978 - 1963) / 2)) * tdat$bw1963to1978 ) + 
((1978 + ((1991 - 1978) / 2)) * tdat$bw1978to1991 ) + 
((1991 + ((2000 - 1991) / 2)) * tdat$bw1991to2000 ) + 
((2000 + ((2008 - 2000) / 2)) * tdat$bw2000to2008 ) + 
((2008 + ((2014 - 2008) / 2)) * tdat$bw2008to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="luanda-3050",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)



#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['primary', 'trunk']", "['tertiary', 'primary_link']", "['tertiary', 'secondary_link']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1963
test <- over(boulevards, t1963, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1963 <- 0
boulevards$pre_1963[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1963)

#bw1963to1978
test <- over(boulevards, t1978, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1963to1978 <- 0
boulevards$bw1963to1978[is.na(test$id)==F & boulevards$pre_1963 ==0] <- 1
table(boulevards$pre_1963, boulevards$bw1963to1978)

#bw1978to1991
test <- over(boulevards, t1991, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1978to1991 <- 0
boulevards$bw1978to1991[is.na(test$id)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1978 ==0] <- 1
table(boulevards$bw1963to1978, boulevards$bw1978to1991)


#bw1991to2000
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1991to2000 <- 0
boulevards$bw1991to2000[is.na(test$id)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1978 ==0 & boulevards$bw1978to1991 ==0] <- 1
table(boulevards$bw1978to1991, boulevards$bw1991to2000)

#bw2000to2008
test <- over(boulevards, t2008, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2008 <- 0
boulevards$bw2000to2008[is.na(test$id)==F  & boulevards$pre_1963 ==0 & boulevards$bw1963to1978 ==0 & boulevards$bw1978to1991 ==0 & boulevards$bw1991to2000 ==0] <- 1
table(boulevards$bw1991to2000, boulevards$bw2000to2008)

#bw2008to2014
test <- over(boulevards, t2014, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2008to2014 <- 0
boulevards$bw2008to2014[is.na(test$id)==F & boulevards$pre_1963 ==0 & boulevards$bw1963to1978 ==0 & boulevards$bw1978to1991 ==0 & boulevards$bw1991to2000 ==0 & boulevards$bw2000to2008 ==0] <- 1
table(boulevards$bw2000to2008, boulevards$bw2008to2014)


#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1963 ==0 & boulevards$bw1963to1978 ==0 & boulevards$bw1978to1991 ==0 & boulevards$bw1991to2000 ==0 & boulevards$bw2000to2008 ==0 & boulevards$bw2008to2014 ==0] <- 1




#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[22], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km




save(tdat, file=paste("./boeing_data/tcommunities/", "luanda-3050", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 22: LUBUMBASHI ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/democratic_republic_of_the_congo-COD_gpkg/lubumbashi-3756.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/democratic_republic_of_the_congo-COD_gpkg/lubumbashi-3756.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "lubumbashi-3756", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "lubumbashi-3756", ".Rdata", sep="")	)


#urban extent at 1954
t1954 <- readOGR("./historical_maps_todigitize/lubumbashi/lubumbashi1954.shp")
class(t1954)
proj4string(t1954)

#urban extent at 1969
t1969 <- readOGR("./extents/lubumbashi/Lubumbashi 1969 Merged.shp")
class(t1969)
proj4string(t1969)
t1969@data <- as.data.frame(matrix(1, nrow=nrow(t1969), ncol=1))
colnames(t1969@data) <- c("id")

#urban extent at 1985
t1985 <- readOGR("./extents/lubumbashi/lubumbashi1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1990
t1990 <- readOGR("./extents/lubumbashi/lubumbashi1990.shp")
class(t1990)
proj4string(t1990)

#urban extent at 1998
t1998 <- readOGR("./extents/lubumbashi/lubumbashi1998.shp")
class(t1998)
proj4string(t1998)

#urban extent at 2006
t2006 <- readOGR("./extents/lubumbashi/lubumbashi2006.shp")
class(t2006)
proj4string(t2006)

#urban extent at 2013
t2013 <- readOGR("./extents/lubumbashi/lubumbashi2013.shp")
class(t2013)
proj4string(t2013)

#pre_1954
test <- over(nodes, t1954)
length(test)
dim(nodes)
head(test)
nodes$pre_1954 <- 0
nodes$pre_1954[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1954)

#bw1954to1969
test <- over(nodes, t1969)
dim(test)
dim(nodes)
head(test)
nodes$bw1954to1969 <- 0
nodes$bw1954to1969[is.na(test$id)==F & nodes$pre_1954 ==0] <- 1
table(nodes$pre_1954, nodes$bw1954to1969)

#bw1969to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1969to1985 <- 0
nodes$bw1969to1985[is.na(test$id)==F & nodes$pre_1954 ==0 & nodes$bw1954to1969 ==0] <- 1
table(nodes$bw1954to1969, nodes$bw1969to1985)

#bw1985to1990
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1990 <- 0
nodes$bw1985to1990[is.na(test$id)==F & nodes$pre_1954 ==0 & nodes$bw1954to1969 ==0 & nodes$bw1969to1985 ==0] <- 1
table(nodes$bw1969to1985, nodes$bw1985to1990)

#bw1990to1998
test <- over(nodes, t1998)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to1998 <- 0
nodes$bw1990to1998[is.na(test$id)==F & nodes$pre_1954 ==0 & nodes$bw1954to1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1990 ==0] <- 1
table(nodes$bw1985to1990, nodes$bw1990to1998)

#bw1998to2006
test <- over(nodes, t2006)
dim(test)
dim(nodes)
head(test)
nodes$bw1998to2006 <- 0
nodes$bw1998to2006[is.na(test$id)==F & nodes$pre_1954 ==0 & nodes$bw1954to1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2006)

#bw2006to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2006to2013 <- 0
nodes$bw2006to2013[is.na(test$id)==F & nodes$pre_1954 ==0 & nodes$bw1954to1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0  & nodes$bw1998to2006 ==0] <- 1
table(nodes$bw1998to2006, nodes$bw2006to2013)


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[  nodes$pre_1954 ==0 & nodes$bw1954to1969 ==0 & nodes$bw1969to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0  & nodes$bw1998to2006 ==0 & nodes$bw2006to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1954 <- NA
tdat$bw1954to1969 <- NA
tdat$bw1969to1985 <- NA
tdat$bw1985to1990 <- NA
tdat$bw1990to1998 <- NA
tdat$bw1998to2006 <- NA
tdat$bw2006to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1954[i] <- mean(nodes@data$pre_1954[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1954to1969[i] <- mean(nodes@data$bw1954to1969[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1969to1985[i] <- mean(nodes@data$bw1969to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1990[i] <- mean(nodes@data$bw1985to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to1998[i] <- mean(nodes@data$bw1990to1998[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1998to2006[i] <- mean(nodes@data$bw1998to2006[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2006to2013[i] <- mean(nodes@data$bw2006to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1910 #date of founding
tdat$average_age <- ((historical_anchor + ((1954 - historical_anchor) / 2)) * tdat$pre_1954 ) + 
((1954 + ((1969 - 1954) / 2)) * tdat$bw1954to1969 ) + 
((1969 + ((1985 - 1969) / 2)) * tdat$bw1969to1985 ) + 
((1985 + ((1990 - 1985) / 2)) * tdat$bw1985to1990 ) + 
((1990 + ((1998 - 1990) / 2)) * tdat$bw1990to1998 ) + 
((1998 + ((2006 - 1998) / 2)) * tdat$bw1998to2006 ) + 
((2006 + ((2013 - 2006) / 2)) * tdat$bw2006to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="lubumbashi-3756",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))


nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)


#pre_1954
test <- over(boulevards, t1954,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1954 <- 0
boulevards$pre_1954[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1954)

#bw1954to1969
test <- over(boulevards, t1969,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1954to1969 <- 0
boulevards$bw1954to1969[is.na(test$id)==F & boulevards$pre_1954 ==0] <- 1
table(boulevards$pre_1954, boulevards$bw1954to1969)

#bw1969to1985
test <- over(boulevards, t1985,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1969to1985 <- 0
boulevards$bw1969to1985[is.na(test$id)==F & boulevards$pre_1954 ==0 & boulevards$bw1954to1969 ==0] <- 1
table(boulevards$bw1954to1969, boulevards$bw1969to1985)

#bw1985to1990
test <- over(boulevards, t1990,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1990 <- 0
boulevards$bw1985to1990[is.na(test$id)==F & boulevards$pre_1954 ==0 & boulevards$bw1954to1969 ==0 & boulevards$bw1969to1985 ==0] <- 1
table(boulevards$bw1969to1985, boulevards$bw1985to1990)

#bw1990to1998
test <- over(boulevards, t1998,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to1998 <- 0
boulevards$bw1990to1998[is.na(test$id)==F & boulevards$pre_1954 ==0 & boulevards$bw1954to1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1990 ==0] <- 1
table(boulevards$bw1985to1990, boulevards$bw1990to1998)

#bw1998to2006
test <- over(boulevards, t2006,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1998to2006 <- 0
boulevards$bw1998to2006[is.na(test$id)==F & boulevards$pre_1954 ==0 & boulevards$bw1954to1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2006)

#bw2006to2013
test <- over(boulevards, t2013,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2006to2013 <- 0
boulevards$bw2006to2013[is.na(test$id)==F & boulevards$pre_1954 ==0 & boulevards$bw1954to1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0  & boulevards$bw1998to2006 ==0] <- 1
table(boulevards$bw1998to2006, boulevards$bw2006to2013)


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[  boulevards$pre_1954 ==0 & boulevards$bw1954to1969 ==0 & boulevards$bw1969to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0  & boulevards$bw1998to2006 ==0 & boulevards$bw2006to2013 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[23], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km





save(tdat, file=paste("./boeing_data/tcommunities/", "lubumbashi-3756", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 23: MALABO ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/equatorial_guinea-GNQ_gpkg/malabo-2762.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/equatorial_guinea-GNQ_gpkg/malabo-2762.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "malabo-2762", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "malabo-2762", ".Rdata", sep="")	)

#urban extent at 1956
t1956 <- readOGR("./extents/malabo/malabo1956.shp")
class(t1956)
proj4string(t1956)

#urban extent at 1985
t1985 <- readOGR("./extents/malabo/malabo1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 2003
t2003 <- readOGR("./extents/malabo/malabo2003.shp")
class(t2003)
proj4string(t2003)

#urban extent at 2007
t2007 <- readOGR("./extents/malabo/malabo2007.shp")
class(t2007)
proj4string(t2007)


#pre_1956
test <- over(nodes, t1956)
length(test)
dim(nodes)
head(test)
nodes$pre_1956 <- 0
nodes$pre_1956[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1956)

#bw1956to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1956to1985 <- 0
nodes$bw1956to1985[is.na(test$id)==F & nodes$pre_1956 ==0] <- 1
table(nodes$pre_1956, nodes$bw1956to1985)

#bw1985to2003
test <- over(nodes, t2003)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to2003 <- 0
nodes$bw1985to2003[is.na(test$id)==F & nodes$pre_1956 ==0 & nodes$bw1956to1985 ==0] <- 1
table(nodes$bw1956to1985, nodes$bw1985to2003)

#bw2003to2007
test <- over(nodes, t2007)
dim(test)
dim(nodes)
head(test)
nodes$bw2003to2007 <- 0
nodes$bw2003to2007[is.na(test$id)==F & nodes$pre_1956 ==0 & nodes$bw1956to1985 ==0 & nodes$bw1985to2003 ==0] <- 1
table(nodes$bw1985to2003, nodes$bw2003to2007)



#bw2007to2020
nodes$bw2007to2020 <- 0
nodes$bw2007to2020[ nodes$pre_1956 ==0 & nodes$bw1956to1985 ==0 & nodes$bw1985to2003 ==0 & nodes$bw2003to2007 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1956 <- NA
tdat$bw1956to1985 <- NA
tdat$bw1985to2003 <- NA
tdat$bw2003to2007 <- NA
tdat$bw2007to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1956[i] <- mean(nodes@data$pre_1956[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1956to1985[i] <- mean(nodes@data$bw1956to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to2003[i] <- mean(nodes@data$bw1985to2003[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2003to2007[i] <- mean(nodes@data$bw2003to2007[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2007to2020[i] <- mean(nodes@data$bw2007to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1827 #british founding of port clarence (see wikipedia)
tdat$average_age <- ((historical_anchor + ((1956 - historical_anchor) / 2)) * tdat$pre_1956 ) + 
((1956 + ((1985 - 1956) / 2)) * tdat$bw1956to1985 ) + 
((1985 + ((2003 - 1985) / 2)) * tdat$bw1985to2003 ) + 
((2003 + ((2007 - 2003) / 2)) * tdat$bw2003to2007 ) + 
((2007 + ((2020 - 2007) / 2)) * tdat$bw2007to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="malabo-2762",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1956==1] <- dist1[nodes$pre_1956=1,1] 
nodes$dist_to_hq[nodes$bw1985to2003 ==1 | nodes$bw2003to2007 ==1 | nodes$bw2007to2020 ==1 ] <- dist2[nodes$bw1985to2003 ==1 | nodes$bw2003to2007 ==1 | nodes$bw2007to2020 ==1, 1] 
nodes$dist_to_hq[nodes$bw1956to1985 ==1] <- (dist1[nodes$bw1956to1985 ==1,1] * (9/29)) + (dist2[nodes$bw1956to1985 ==1,1] * (20/29)) #just the time period that is crossed the swithceroo
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1956
test <- over(boulevards, t1956, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1956 <- 0
boulevards$pre_1956[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1956)

#bw1956to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1956to1985 <- 0
boulevards$bw1956to1985[is.na(test$id)==F & boulevards$pre_1956 ==0] <- 1
table(boulevards$pre_1956, boulevards$bw1956to1985)

#bw1985to2003
test <- over(boulevards, t2003, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to2003 <- 0
boulevards$bw1985to2003[is.na(test$id)==F & boulevards$pre_1956 ==0 & boulevards$bw1956to1985 ==0] <- 1
table(boulevards$bw1956to1985, boulevards$bw1985to2003)

#bw2003to2007
test <- over(boulevards, t2007, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2003to2007 <- 0
boulevards$bw2003to2007[is.na(test$id)==F & boulevards$pre_1956 ==0 & boulevards$bw1956to1985 ==0 & boulevards$bw1985to2003 ==0] <- 1
table(boulevards$bw1985to2003, boulevards$bw2003to2007)

#bw2007to2020
boulevards$bw2007to2020 <- 0
boulevards$bw2007to2020[ boulevards$pre_1956 ==0 & boulevards$bw1956to1985 ==0 & boulevards$bw1985to2003 ==0 & boulevards$bw2003to2007 ==0] <- 1




graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[24], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km





save(tdat, file=paste("./boeing_data/tcommunities/", "malabo-2762", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 24: MANZINI ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/swaziland-SWZ_gpkg/manzini-4080.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/swaziland-SWZ_gpkg/manzini-4080.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "manzini-4080", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "manzini-4080", ".Rdata", sep="")	)



#urban extent at 1958
t1958 <- readOGR("./extents/manzini/manzini1958.shp")
class(t1958)
proj4string(t1958)

#urban extent at 1979
t1979 <- readOGR("./extents/manzini/manzini1979.shp")
class(t1979)
proj4string(t1979)

#urban extent at 1995
t1995 <- readOGR("./extents/manzini/manzini1995.shp")
class(t1995)
proj4string(t1995)

#urban extent at 2009
t2009 <- readOGR("./extents/manzini/manzini2009.shp")
class(t2009)
proj4string(t2009)

#pre_1958
test <- over(nodes, t1958)
length(test)
dim(nodes)
head(test)
nodes$pre_1958 <- 0
nodes$pre_1958[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1958)

#bw1958to1979
test <- over(nodes, t1979)
dim(test)
dim(nodes)
head(test)
nodes$bw1958to1979 <- 0
nodes$bw1958to1979[is.na(test$id)==F & nodes$pre_1958 ==0] <- 1
table(nodes$pre_1958, nodes$bw1958to1979)

#bw1979to1995
test <- over(nodes, t1995)
dim(test)
dim(nodes)
head(test)
nodes$bw1979to1995 <- 0
nodes$bw1979to1995[is.na(test$id)==F & nodes$pre_1958 ==0 & nodes$bw1958to1979 ==0] <- 1
table(nodes$bw1958to1979, nodes$bw1979to1995)

#bw1995to2009
test <- over(nodes, t2009)
dim(test)
dim(nodes)
head(test)
nodes$bw1995to2009 <- 0
nodes$bw1995to2009[is.na(test$id)==F & nodes$pre_1958 ==0 & nodes$bw1958to1979 ==0 & nodes$bw1979to1995 ==0] <- 1
table(nodes$bw1979to1995, nodes$bw1995to2009)

#bw2009to2020
nodes$bw2009to2020 <- 0
nodes$bw2009to2020[ nodes$pre_1958 ==0 & nodes$bw1958to1979 ==0 & nodes$bw1979to1995 ==0 & nodes$bw1995to2009 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1958 <- NA
tdat$bw1958to1979 <- NA
tdat$bw1979to1995 <- NA
tdat$bw1995to2009 <- NA
tdat$bw2009to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1958[i] <- mean(nodes@data$pre_1958[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1958to1979[i] <- mean(nodes@data$bw1958to1979[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1979to1995[i] <- mean(nodes@data$bw1979to1995[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1995to2009[i] <- mean(nodes@data$bw1995to2009[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2009to2020[i] <- mean(nodes@data$bw2009to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1885 #wikipedia date
tdat$average_age <- ((historical_anchor + ((1958 - historical_anchor) / 2)) * tdat$pre_1958 ) + 
((1958 + ((1979 - 1958) / 2)) * tdat$bw1958to1979 ) + 
((1979 + ((1995 - 1979) / 2)) * tdat$bw1979to1995 ) + 
((1995 + ((2009 - 1995) / 2)) * tdat$bw1995to2009 ) +
((2009 + ((2020 - 2009) / 2)) * tdat$bw2009to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="manzini-4080",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)





#pre_1958
test <- over(boulevards, t1958,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1958 <- 0
boulevards$pre_1958[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1958)

#bw1958to1979
test <- over(boulevards, t1979,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1958to1979 <- 0
boulevards$bw1958to1979[is.na(test$id)==F & boulevards$pre_1958 ==0] <- 1
table(boulevards$pre_1958, boulevards$bw1958to1979)

#bw1979to1995
test <- over(boulevards, t1995,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1979to1995 <- 0
boulevards$bw1979to1995[is.na(test$id)==F & boulevards$pre_1958 ==0 & boulevards$bw1958to1979 ==0] <- 1
table(boulevards$bw1958to1979, boulevards$bw1979to1995)

#bw1995to2009
test <- over(boulevards, t2009,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1995to2009 <- 0
boulevards$bw1995to2009[is.na(test$id)==F & boulevards$pre_1958 ==0 & boulevards$bw1958to1979 ==0 & boulevards$bw1979to1995 ==0] <- 1
table(boulevards$bw1979to1995, boulevards$bw1995to2009)

#bw2009to2020
boulevards$bw2009to2020 <- 0
boulevards$bw2009to2020[ boulevards$pre_1958 ==0 & boulevards$bw1958to1979 ==0 & boulevards$bw1979to1995 ==0 & boulevards$bw1995to2009 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[25], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "manzini-4080", "_tdat_wyears_NEW", ".Rdata", sep="")	)

#####################################
##### CITY 25: MAPUTO ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/mozambique-MOZ_gpkg/maputo-4220.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/mozambique-MOZ_gpkg/maputo-4220.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "maputo-4220", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "maputo-4220", ".Rdata", sep="")	)


#urban extent at 1930
t1930 <- readOGR("./extents/maputo/maputo1930.shp")
class(t1930)
proj4string(t1930)

#urban extent at 1967
t1967 <- readOGR("./extents/maputo/maputo1967.shp")
class(t1967)
proj4string(t1967)

#urban extent at 1986
t1986 <- readOGR("./extents/maputo/maputo1986.shp")
class(t1986)
proj4string(t1986)

#urban extent at 2004
t2004 <- readOGR("./extents/maputo/maputo2004.shp")
class(t2004)
proj4string(t2004)

#urban extent at 2011
t2011 <- readOGR("./extents/maputo/maputo2011.shp")
class(t2011)
proj4string(t2011)

#pre_1930
test <- over(nodes, t1930)
length(test)
dim(nodes)
head(test)
nodes$pre_1930 <- 0
nodes$pre_1930[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1930)

#bw1930to1967
test <- over(nodes, t1967)
dim(test)
dim(nodes)
head(test)
nodes$bw1930to1967 <- 0
nodes$bw1930to1967[is.na(test$id)==F & nodes$pre_1930 ==0] <- 1
table(nodes$pre_1930, nodes$bw1930to1967)

#bw1967to1986
test <- over(nodes, t1986)
dim(test)
dim(nodes)
head(test)
nodes$bw1967to1986 <- 0
nodes$bw1967to1986[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1967 ==0] <- 1
table(nodes$bw1930to1967, nodes$bw1967to1986)

#bw1986to2004
test <- over(nodes, t2004)
dim(test)
dim(nodes)
head(test)
nodes$bw1986to2004 <- 0
nodes$bw1986to2004[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1967 ==0 & nodes$bw1967to1986 ==0] <- 1
table(nodes$bw1967to1986, nodes$bw1986to2004)

#bw2004to2011
test <- over(nodes, t2011)
dim(test)
dim(nodes)
head(test)
nodes$bw2004to2011 <- 0
nodes$bw2004to2011[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1967 ==0 & nodes$bw1967to1986 ==0 & nodes$bw1986to2004 ==0] <- 1
table(nodes$bw1986to2004, nodes$bw2004to2011)


#bw2011to2020
nodes$bw2011to2020 <- 0
nodes$bw2011to2020[ nodes$pre_1930 ==0 & nodes$bw1930to1967 ==0 & nodes$bw1967to1986 ==0 & nodes$bw1986to2004 ==0 & nodes$bw2004to2011 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1930 <- NA
tdat$bw1930to1967 <- NA
tdat$bw1967to1986 <- NA
tdat$bw1986to2004 <- NA
tdat$bw2004to2011 <- NA
tdat$bw2011to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1930[i] <- mean(nodes@data$pre_1930[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1930to1967[i] <- mean(nodes@data$bw1930to1967[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1967to1986[i] <- mean(nodes@data$bw1967to1986[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1986to2004[i] <- mean(nodes@data$bw1986to2004[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2004to2011[i] <- mean(nodes@data$bw2004to2011[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2011to2020[i] <- mean(nodes@data$bw2011to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1850 #wikipedia says existing town dates from then, earlier versions destroyed
tdat$average_age <- ((historical_anchor + ((1959 - historical_anchor) / 2)) * tdat$pre_1930 ) + 
((1959 + ((1964 - 1959) / 2)) * tdat$bw1930to1967 ) + 
((1964 + ((1973 - 1964) / 2)) * tdat$bw1967to1986 ) + 
((1973 + ((1986 - 1973) / 2)) * tdat$bw1986to2004 ) + 
((1986 + ((1992 - 1986) / 2)) * tdat$bw2004to2011 ) +
((1986 + ((1992 - 1986) / 2)) * tdat$bw2011to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="maputo-4220",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1930
test <- over(boulevards, t1930)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1930 <- 0
boulevards$pre_1930[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1930)

#bw1930to1967
test <- over(boulevards, t1967)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1930to1967 <- 0
boulevards$bw1930to1967[is.na(test$id)==F & boulevards$pre_1930 ==0] <- 1
table(boulevards$pre_1930, boulevards$bw1930to1967)

#bw1967to1986
test <- over(boulevards, t1986)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1967to1986 <- 0
boulevards$bw1967to1986[is.na(test$id)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1967 ==0] <- 1
table(boulevards$bw1930to1967, boulevards$bw1967to1986)

#bw1986to2004
test <- over(boulevards, t2004)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1986to2004 <- 0
boulevards$bw1986to2004[is.na(test$id)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1967 ==0 & boulevards$bw1967to1986 ==0] <- 1
table(boulevards$bw1967to1986, boulevards$bw1986to2004)

#bw2004to2011
test <- over(boulevards, t2011)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2004to2011 <- 0
boulevards$bw2004to2011[is.na(test$id)==F & boulevards$pre_1930 ==0 & boulevards$bw1930to1967 ==0 & boulevards$bw1967to1986 ==0 & boulevards$bw1986to2004 ==0] <- 1
table(boulevards$bw1986to2004, boulevards$bw2004to2011)


#bw2011to2020
boulevards$bw2011to2020 <- 0
boulevards$bw2011to2020[ boulevards$pre_1930 ==0 & boulevards$bw1930to1967 ==0 & boulevards$bw1967to1986 ==0 & boulevards$bw1986to2004 ==0 & boulevards$bw2004to2011 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[26], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard", "ref" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km
table(is.na(tdat$avg_dist_to_boulevard))


save(tdat, file=paste("./boeing_data/tcommunities/", "maputo-4220", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 26: MASERU ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/lesotho-LSO_gpkg/maseru-3631.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/lesotho-LSO_gpkg/maseru-3631.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "maseru-3631", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "maseru-3631", ".Rdata", sep="")	)

#urban extent at 1957
t1957 <- readOGR("./extents/maseru/maseru1957.shp")
class(t1957)
proj4string(t1957)

#urban extent at 1966
t1966 <- readOGR("./extents/maseru/maseru1966.shp")
class(t1966)
proj4string(t1966)

#urban extent at 1975
t1975 <- readOGR("./extents/maseru/maseru1975.shp")
class(t1975)
proj4string(t1975)

#urban extent at 1990
t1990 <- readOGR("./extents/maseru/maseru1990.shp")
class(t1990)
proj4string(t1990)

#urban extent at 2002
t2002 <- readOGR("./extents/maseru/maseru2002.shp")
class(t2002)
proj4string(t2002)

#urban extent at 2010
t2010 <- readOGR("./extents/maseru/maseru2010.shp")
class(t2010)
proj4string(t2010)


#pre_1957
test <- over(nodes, t1957)
length(test)
dim(nodes)
head(test)
nodes$pre_1957 <- 0
nodes$pre_1957[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1957)

#bw1957to1966
test <- over(nodes, t1966)
dim(test)
dim(nodes)
head(test)
nodes$bw1957to1966 <- 0
nodes$bw1957to1966[is.na(test$id)==F & nodes$pre_1957 ==0] <- 1
table(nodes$pre_1957, nodes$bw1957to1966)

#bw1966to1975
test <- over(nodes, t1975)
dim(test)
dim(nodes)
head(test)
nodes$bw1966to1975 <- 0
nodes$bw1966to1975[is.na(test$id)==F & nodes$pre_1957 ==0 & nodes$bw1957to1966 ==0] <- 1
table(nodes$bw1957to1966, nodes$bw1966to1975)

#bw1975to1990
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1975to1990 <- 0
nodes$bw1975to1990[is.na(test$id)==F & nodes$pre_1957 ==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1975 ==0] <- 1
table(nodes$bw1966to1975, nodes$bw1975to1990)

#bw1990to2002
test <- over(nodes, t2002)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to2002 <- 0
nodes$bw1990to2002[is.na(test$id)==F & nodes$pre_1957 ==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1975 ==0 & nodes$bw1975to1990 ==0] <- 1
table(nodes$bw1975to1990, nodes$bw1990to2002)

#bw2002to2010
test <- over(nodes, t2010)
dim(test)
dim(nodes)
head(test)
nodes$bw2002to2010 <- 0
nodes$bw2002to2010[is.na(test$id)==F & nodes$pre_1957 ==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1975 ==0 & nodes$bw1975to1990 ==0 & nodes$bw1990to2002 ==0] <- 1
table(nodes$bw1990to2002, nodes$bw2002to2010)

#bw2010to2020
nodes$bw2010to2020 <- 0
nodes$bw2010to2020[ nodes$pre_1957 ==0 & nodes$bw1957to1966 ==0 & nodes$bw1966to1975 ==0 & nodes$bw1975to1990 ==0 & nodes$bw1990to2002 ==0 & nodes$bw2002to2010 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1957 <- NA
tdat$bw1957to1966 <- NA
tdat$bw1966to1975 <- NA
tdat$bw1975to1990 <- NA
tdat$bw1990to2002 <- NA
tdat$bw2002to2010 <- NA
tdat$bw2010to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1957[i] <- mean(nodes@data$pre_1957[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1957to1966[i] <- mean(nodes@data$bw1957to1966[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1966to1975[i] <- mean(nodes@data$bw1966to1975[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1975to1990[i] <- mean(nodes@data$bw1975to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to2002[i] <- mean(nodes@data$bw1990to2002[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2002to2010[i] <- mean(nodes@data$bw2002to2010[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2010to2020[i] <- mean(nodes@data$bw2010to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1869 #see: wikipedia
tdat$average_age <- ((historical_anchor + ((1957 - historical_anchor) / 2)) * tdat$pre_1957 ) + 
((1957 + ((1966 - 1957) / 2)) * tdat$bw1957to1966 ) + 
((1966 + ((1975 - 1966) / 2)) * tdat$bw1966to1975 ) + 
((1975 + ((1990 - 1975) / 2)) * tdat$bw1975to1990 ) + 
((1990 + ((2002 - 1990) / 2)) * tdat$bw1990to2002 ) + 
((2002 + ((2010 - 2002) / 2)) * tdat$bw2002to2010 ) + 
((2010 + ((2020 - 2010) / 2)) * tdat$bw2010to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="maseru-3631",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1957 ==1 | nodes$bw1957to1966 ==1 ] <- dist1[nodes$pre_1957 ==1 | nodes$bw1957to1966 ==1 ,1] 
nodes$dist_to_hq[nodes$bw1966to1975 ==1 | nodes$bw1975to1990 ==1 | nodes$bw1990to2002 ==1 | nodes$bw2002to2010 ==1 | nodes$bw2010to2020 ==1 ] <- dist2[nodes$bw1966to1975 ==1 | nodes$bw1975to1990 ==1 | nodes$bw1990to2002 ==1 | nodes$bw2002to2010 ==1 | nodes$bw2010to2020 ==1 ,1] 


table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['primary', 'secondary']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1957
test <- over(boulevards, t1957, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1957 <- 0
boulevards$pre_1957[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1957)

#bw1957to1966
test <- over(boulevards, t1966, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1957to1966 <- 0
boulevards$bw1957to1966[is.na(test$id)==F & boulevards$pre_1957 ==0] <- 1
table(boulevards$pre_1957, boulevards$bw1957to1966)

#bw1966to1975
test <- over(boulevards, t1975, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1966to1975 <- 0
boulevards$bw1966to1975[is.na(test$id)==F & boulevards$pre_1957 ==0 & boulevards$bw1957to1966 ==0] <- 1
table(boulevards$bw1957to1966, boulevards$bw1966to1975)

#bw1975to1990
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1975to1990 <- 0
boulevards$bw1975to1990[is.na(test$id)==F & boulevards$pre_1957 ==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1975 ==0] <- 1
table(boulevards$bw1966to1975, boulevards$bw1975to1990)

#bw1990to2002
test <- over(boulevards, t2002, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to2002 <- 0
boulevards$bw1990to2002[is.na(test$id)==F & boulevards$pre_1957 ==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1975 ==0 & boulevards$bw1975to1990 ==0] <- 1
table(boulevards$bw1975to1990, boulevards$bw1990to2002)

#bw2002to2010
test <- over(boulevards, t2010, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2002to2010 <- 0
boulevards$bw2002to2010[is.na(test$id)==F & boulevards$pre_1957 ==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1975 ==0 & boulevards$bw1975to1990 ==0 & boulevards$bw1990to2002 ==0] <- 1
table(boulevards$bw1990to2002, boulevards$bw2002to2010)

#bw2010to2020
boulevards$bw2010to2020 <- 0
boulevards$bw2010to2020[ boulevards$pre_1957 ==0 & boulevards$bw1957to1966 ==0 & boulevards$bw1966to1975 ==0 & boulevards$bw1975to1990 ==0 & boulevards$bw1990to2002 ==0 & boulevards$bw2002to2010 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[27], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "maseru-3631", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 27: MONROVIA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/liberia-LBR_gpkg/monrovia-1526.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/liberia-LBR_gpkg/monrovia-1526.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "monrovia-1526", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "monrovia-1526", ".Rdata", sep="")	)

#urban extent at 1946
t1946 <- readOGR("./extents/monrovia/monrovia1946.shp")
class(t1946)
proj4string(t1946)

#urban extent at 1955
t1955 <- readOGR("./extents/monrovia/monrovia1955.shp")
class(t1955)
proj4string(t1955)

#urban extent at 1975
t1975 <- readOGR("./extents/monrovia/monrovia1975.shp")
class(t1975)
proj4string(t1975)

#urban extent at 1984
t1984 <- readOGR("./extents/monrovia/monrovia1984.shp")
class(t1984)
proj4string(t1984)

#urban extent at 1992
t1992 <- readOGR("./extents/monrovia/monrovia1992.shp")
class(t1992)
proj4string(t1992)

#urban extent at 2008
t2008 <- readOGR("./extents/monrovia/monrovia2008.shp")
class(t2008)
proj4string(t2008)

#urban extent at 2015
t2015 <- readOGR("./extents/monrovia/monrovia2015.shp")
class(t2015)
proj4string(t2015)

#pre_1946
test <- over(nodes, t1946)
length(test)
dim(nodes)
head(test)
nodes$pre_1946 <- 0
nodes$pre_1946[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1946)

#bw1946to1955
test <- over(nodes, t1955)
dim(test)
dim(nodes)
head(test)
nodes$bw1946to1955 <- 0
nodes$bw1946to1955[is.na(test$id)==F & nodes$pre_1946 ==0] <- 1
table(nodes$pre_1946, nodes$bw1946to1955)

#bw1955to1975
test <- over(nodes, t1975)
dim(test)
dim(nodes)
head(test)
nodes$bw1955to1975 <- 0
nodes$bw1955to1975[is.na(test$id)==F & nodes$pre_1946 ==0 & nodes$bw1946to1955 ==0] <- 1
table(nodes$bw1946to1955, nodes$bw1955to1975)

#bw1975to1984
test <- over(nodes, t1984)
dim(test)
dim(nodes)
head(test)
nodes$bw1975to1984 <- 0
nodes$bw1975to1984[is.na(test$id)==F & nodes$pre_1946 ==0 & nodes$bw1946to1955 ==0 & nodes$bw1955to1975 ==0] <- 1
table(nodes$bw1955to1975, nodes$bw1975to1984)

#bw1984to1992
test <- over(nodes, t1992)
dim(test)
dim(nodes)
head(test)
nodes$bw1984to1992 <- 0
nodes$bw1984to1992[is.na(test$id)==F & nodes$pre_1946 ==0 & nodes$bw1946to1955 ==0 & nodes$bw1955to1975 ==0 & nodes$bw1975to1984 ==0] <- 1
table(nodes$bw1975to1984, nodes$bw1984to1992)

#bw1992to2008
test <- over(nodes, t2008)
dim(test)
dim(nodes)
head(test)
nodes$bw1992to2008 <- 0
nodes$bw1992to2008[is.na(test$id)==F & nodes$pre_1946 ==0 & nodes$bw1946to1955 ==0 & nodes$bw1955to1975 ==0 & nodes$bw1975to1984 ==0 & nodes$bw1984to1992 ==0] <- 1
table(nodes$bw1984to1992, nodes$bw1992to2008)

#bw2008to2015
test <- over(nodes, t2015)
dim(test)
dim(nodes)
head(test)
nodes$bw2008to2015 <- 0
nodes$bw2008to2015[is.na(test$id)==F & nodes$pre_1946 ==0 & nodes$bw1946to1955 ==0 & nodes$bw1955to1975 ==0 & nodes$bw1975to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2008 ==0] <- 1
table(nodes$bw1992to2008, nodes$bw2008to2015)


#bw2015to2020
nodes$bw2015to2020 <- 0
nodes$bw2015to2020[ nodes$pre_1946 ==0 & nodes$bw1946to1955 ==0 & nodes$bw1955to1975 ==0 & nodes$bw1975to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2008 ==0 & nodes$bw2008to2015 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1946 <- NA
tdat$bw1946to1955 <- NA
tdat$bw1955to1975 <- NA
tdat$bw1975to1984 <- NA
tdat$bw1984to1992 <- NA
tdat$bw1992to2008 <- NA
tdat$bw2008to2015 <- NA
tdat$bw2015to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1946[i] <- mean(nodes@data$pre_1946[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1946to1955[i] <- mean(nodes@data$bw1946to1955[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1955to1975[i] <- mean(nodes@data$bw1955to1975[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1975to1984[i] <- mean(nodes@data$bw1975to1984[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1984to1992[i] <- mean(nodes@data$bw1984to1992[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1992to2008[i] <- mean(nodes@data$bw1992to2008[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2008to2015[i] <- mean(nodes@data$bw2008to2015[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2015to2020[i] <- mean(nodes@data$bw2015to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1822 #wikipedia
tdat$average_age <- ((historical_anchor + ((1946 - historical_anchor) / 2)) * tdat$pre_1946 ) + 
((1946 + ((1955 - 1946) / 2)) * tdat$bw1946to1955 ) + 
((1955 + ((1975 - 1955) / 2)) * tdat$bw1955to1975 ) + 
((1975 + ((1984 - 1975) / 2)) * tdat$bw1975to1984 ) + 
((1984 + ((1992 - 1984) / 2)) * tdat$bw1984to1992 ) + 
((1992 + ((2008 - 1992) / 2)) * tdat$bw1992to2008 ) + 
((2008 + ((2015 - 2008) / 2)) * tdat$bw2008to2015 ) + 
((2015 + ((2020 - 2015) / 2)) * tdat$bw2015to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="monrovia-1526",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

caps.pts2 <- SpatialPoints(caps[,c("cap_x2","cap_y2")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts2.aedc <- spTransform(caps.pts2, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)
dist2 <- gDistance(caps.pts2.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq[nodes$pre_1946 ==1 | nodes$bw1946to1955 ==1] <- dist1[nodes$pre_1946 ==1 | nodes$bw1946to1955 ==1 , 1]
nodes$dist_to_hq[nodes$bw1975to1984 ==1 | nodes$bw1984to1992 ==1 | nodes$bw1992to2008 ==1 | nodes$bw2008to2015 ==1 | nodes$bw2015to2020 ==1  ] <- dist2[nodes$bw1975to1984 ==1 | nodes$bw1984to1992 ==1 | nodes$bw1992to2008 ==1 | nodes$bw2008to2015 ==1 | nodes$bw2015to2020 ==1  , 1]
 nodes$dist_to_hq[nodes$bw1955to1975 ==1] <- (dist1[nodes$bw1955to1975 ==1,1] * (9/30)) + (dist2[nodes$bw1955to1975 ==1,1] * (21/30)) #just the time period that is crossed the swithceroo
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1946
test <- over(boulevards, t1946, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1946 <- 0
boulevards$pre_1946[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1946)

#bw1946to1955
test <- over(boulevards, t1955, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1946to1955 <- 0
boulevards$bw1946to1955[is.na(test$id)==F & boulevards$pre_1946 ==0] <- 1
table(boulevards$pre_1946, boulevards$bw1946to1955)

#bw1955to1975
test <- over(boulevards, t1975, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1955to1975 <- 0
boulevards$bw1955to1975[is.na(test$id)==F & boulevards$pre_1946 ==0 & boulevards$bw1946to1955 ==0] <- 1
table(boulevards$bw1946to1955, boulevards$bw1955to1975)

#bw1975to1984
test <- over(boulevards, t1984, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1975to1984 <- 0
boulevards$bw1975to1984[is.na(test$id)==F & boulevards$pre_1946 ==0 & boulevards$bw1946to1955 ==0 & boulevards$bw1955to1975 ==0] <- 1
table(boulevards$bw1955to1975, boulevards$bw1975to1984)

#bw1984to1992
test <- over(boulevards, t1992, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1984to1992 <- 0
boulevards$bw1984to1992[is.na(test$id)==F & boulevards$pre_1946 ==0 & boulevards$bw1946to1955 ==0 & boulevards$bw1955to1975 ==0 & boulevards$bw1975to1984 ==0] <- 1
table(boulevards$bw1975to1984, boulevards$bw1984to1992)

#bw1992to2008
test <- over(boulevards, t2008, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1992to2008 <- 0
boulevards$bw1992to2008[is.na(test$id)==F & boulevards$pre_1946 ==0 & boulevards$bw1946to1955 ==0 & boulevards$bw1955to1975 ==0 & boulevards$bw1975to1984 ==0 & boulevards$bw1984to1992 ==0] <- 1
table(boulevards$bw1984to1992, boulevards$bw1992to2008)

#bw2008to2015
test <- over(boulevards, t2015, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2008to2015 <- 0
boulevards$bw2008to2015[is.na(test$id)==F & boulevards$pre_1946 ==0 & boulevards$bw1946to1955 ==0 & boulevards$bw1955to1975 ==0 & boulevards$bw1975to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2008 ==0] <- 1
table(boulevards$bw1992to2008, boulevards$bw2008to2015)

#bw2015to2020
boulevards$bw2015to2020 <- 0
boulevards$bw2015to2020[ boulevards$pre_1946 ==0 & boulevards$bw1946to1955 ==0 & boulevards$bw1955to1975 ==0 & boulevards$bw1975to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2008 ==0 & boulevards$bw2008to2015 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[28], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "monrovia-1526", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 28: NAIROBI ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/kenya-KEN_gpkg/nairobi-4808.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/kenya-KEN_gpkg/nairobi-4808.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "nairobi-4808", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "nairobi-4808", ".Rdata", sep="")	)

#urban extent at 1948
t1948 <- readOGR("./extents/nairobi/Nairobi1948.shp")
class(t1948)
proj4string(t1948)
t1948 <- spTransform(t1948, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 1962
t1962 <- readOGR("./extents/nairobi/Nairobi1962_v2.shp")
class(t1962)
proj4string(t1962)
t1962 <- spTransform(t1962, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 1978
t1978 <- readOGR("./extents/nairobi/nairobi1978_cleaned.shp")
class(t1978)
proj4string(t1978)
t1978 <- spTransform(t1978, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 2004
t2004 <- readOGR("./extents/nairobi/nairobi2004.shp")
class(t2004)
proj4string(t2004)
t2004 <- spTransform(t2004, CRS(" +proj=longlat +datum=WGS84 +no_defs "))

#urban extent at 2014
t2014 <- readOGR("./extents/nairobi/urbFootprint_t3_subset_pruned.shp")
class(t2014)
proj4string(t2014)

#pre_1948
test <- over(nodes, t1948)
length(test)
dim(nodes)
head(test)
nodes$pre_1948 <- 0
nodes$pre_1948[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1948)

#bw1948to1962
test <- over(nodes, t1962)
dim(test)
dim(nodes)
head(test)
nodes$bw1948to1962 <- 0
nodes$bw1948to1962[is.na(test$id)==F & nodes$pre_1948 ==0] <- 1
table(nodes$pre_1948, nodes$bw1948to1962)

#bw1962to1978
test <- over(nodes, t1978)
dim(test)
dim(nodes)
head(test)
nodes$bw1962to1978 <- 0
nodes$bw1962to1978[is.na(test$id)==F & nodes$pre_1948 ==0 & nodes$bw1948to1962 ==0] <- 1
table(nodes$bw1948to1962, nodes$bw1962to1978)

#bw1978to2004
test <- over(nodes, t2004)
dim(test)
dim(nodes)
head(test)
nodes$bw1978to2004 <- 0
nodes$bw1978to2004[is.na(test$id)==F & nodes$pre_1948 ==0 & nodes$bw1948to1962 ==0 & nodes$bw1962to1978 ==0] <- 1
table(nodes$bw1962to1978, nodes$bw1978to2004)

#bw2004to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2004to2014 <- 0
nodes$bw2004to2014[is.na(test$fid)==F & nodes$pre_1948 ==0 & nodes$bw1948to1962 ==0 & nodes$bw1962to1978 ==0 & nodes$bw1978to2004 ==0] <- 1
table(nodes$bw1978to2004, nodes$bw2004to2014)


#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1948 ==0 & nodes$bw1948to1962 ==0 & nodes$bw1962to1978 ==0 & nodes$bw1978to2004 ==0 & nodes$bw2004to2014 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1948 <- NA
tdat$bw1948to1962 <- NA
tdat$bw1962to1978 <- NA
tdat$bw1978to2004 <- NA
tdat$bw2004to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1948[i] <- mean(nodes@data$pre_1948[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1948to1962[i] <- mean(nodes@data$bw1948to1962[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1962to1978[i] <- mean(nodes@data$bw1962to1978[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1978to2004[i] <- mean(nodes@data$bw1978to2004[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2004to2014[i] <- mean(nodes@data$bw2004to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1898 #see: wikipedia
tdat$average_age <- ((historical_anchor + ((1948 - historical_anchor) / 2)) * tdat$pre_1948 ) + 
((1948 + ((1962 - 1948) / 2)) * tdat$bw1948to1962 ) + 
((1962 + ((1978 - 1962) / 2)) * tdat$bw1962to1978 ) + 
((1978 + ((2004 - 1978) / 2)) * tdat$bw1978to2004 ) + 
((2004 + ((2014 - 2004) / 2)) * tdat$bw2004to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)


#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="nairobi-4808",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[ ,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['primary', 'primary_link']", "['residential', 'secondary']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)


#pre_1948
test <- over(boulevards, t1948,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1948 <- 0
boulevards$pre_1948[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1948)

#bw1948to1962
test <- over(boulevards, t1962,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1948to1962 <- 0
boulevards$bw1948to1962[is.na(test$id)==F & boulevards$pre_1948 ==0] <- 1
table(boulevards$pre_1948, boulevards$bw1948to1962)

#bw1962to1978
test <- over(boulevards, t1978,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1962to1978 <- 0
boulevards$bw1962to1978[is.na(test$id)==F & boulevards$pre_1948 ==0 & boulevards$bw1948to1962 ==0] <- 1
table(boulevards$bw1948to1962, boulevards$bw1962to1978)

#bw1978to2004
test <- over(boulevards, t2004,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1978to2004 <- 0
boulevards$bw1978to2004[is.na(test$id)==F & boulevards$pre_1948 ==0 & boulevards$bw1948to1962 ==0 & boulevards$bw1962to1978 ==0] <- 1
table(boulevards$bw1962to1978, boulevards$bw1978to2004)

#bw2004to2014
test <- over(boulevards, t2014,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2004to2014 <- 0
boulevards$bw2004to2014[is.na(test$fid)==F & boulevards$pre_1948 ==0 & boulevards$bw1948to1962 ==0 & boulevards$bw1962to1978 ==0 & boulevards$bw1978to2004 ==0] <- 1
table(boulevards$bw1978to2004, boulevards$bw2004to2014)


#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1948 ==0 & boulevards$bw1948to1962 ==0 & boulevards$bw1962to1978 ==0 & boulevards$bw1978to2004 ==0 & boulevards$bw2004to2014 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[29], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" , "ref")]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "nairobi-4808", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 4: NAKURU ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/kenya-KEN_gpkg/nakuru-4729.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/kenya-KEN_gpkg/nakuru-4729.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "nakuru-4729", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "nakuru-4729", ".Rdata", sep="")	)


#urban extent at 1961
t1961 <- readOGR("./extents/nakuru/nakuru1961.shp")
class(t1961)
proj4string(t1961)

#urban extent at 1975
t1975 <- readOGR("./extents/nakuru/nakuru1975.shp")
class(t1975)
proj4string(t1975)

#urban extent at 1985
t1985 <- readOGR("./extents/nakuru/nakuru1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1989
t1989 <- readOGR("./extents/nakuru/urbFootprint_t1_subset_pruned.shp")
class(t1989)
proj4string(t1989)

#urban extent at 2000
t2000 <- readOGR("./extents/nakuru/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2006
t2006 <- readOGR("./extents/nakuru/nakuru2006.shp")
class(t2006)
proj4string(t2006)

#urban extent at 2014
t2014 <- readOGR("./extents/nakuru/urbFootprint_t3_subset_pruned.shp")
class(t2014)
proj4string(t2014)

#pre_1961
test <- over(nodes, t1961)
length(test)
dim(nodes)
head(test)
nodes$pre_1961 <- 0
nodes$pre_1961[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1961)

#bw1961to1975
test <- over(nodes, t1975)
dim(test)
dim(nodes)
head(test)
nodes$bw1961to1975 <- 0
nodes$bw1961to1975[is.na(test$id)==F & nodes$pre_1961 ==0] <- 1
table(nodes$pre_1961, nodes$bw1961to1975)


#bw1975to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1975to1985 <- 0
nodes$bw1975to1985[is.na(test$id)==F & nodes$pre_1961 ==0 & nodes$bw1961to1975 ==0] <- 1
table(nodes$bw1961to1975, nodes$bw1975to1985)


#bw1985to1989
test <- over(nodes, t1989)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1989 <- 0
nodes$bw1985to1989[is.na(test$fid)==F & nodes$pre_1961 ==0 & nodes$bw1961to1975 ==0 & nodes$bw1975to1985 ==0] <- 1
table(nodes$bw1975to1985, nodes$bw1985to1989)


#bw1989to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1989to2000 <- 0
nodes$bw1989to2000[is.na(test$fid)==F & nodes$pre_1961 ==0 & nodes$bw1961to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1989 ==0] <- 1
table(nodes$bw1985to1989, nodes$bw1989to2000)

#bw2000to2006
test <- over(nodes, t2006)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2006 <- 0
nodes$bw2000to2006[is.na(test$id)==F & nodes$pre_1961 ==0 & nodes$bw1961to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1989 ==0 & nodes$bw1989to2000 ==0] <- 1
table(nodes$bw1989to2000, nodes$bw2000to2006)


#bw2006to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2006to2014 <- 0
nodes$bw2006to2014[is.na(test$fid)==F & nodes$pre_1961 ==0 & nodes$bw1961to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1989 ==0 & nodes$bw1989to2000 ==0 & nodes$bw2000to2006 ==0] <- 1
table(nodes$bw2000to2006, nodes$bw2006to2014)



#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1961 ==0 & nodes$bw1961to1975 ==0 & nodes$bw1975to1985 ==0 & nodes$bw1985to1989 ==0 & nodes$bw1989to2000 ==0 & nodes$bw2000to2006 ==0 & nodes$bw2006to2014 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1961 <- NA
tdat$bw1961to1975 <- NA
tdat$bw1975to1985 <- NA
tdat$bw1985to1989 <- NA
tdat$bw1989to2000 <- NA
tdat$bw2000to2006 <- NA
tdat$bw2006to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1961[i] <- mean(nodes@data$pre_1961[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1961to1975[i] <- mean(nodes@data$bw1961to1975[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1975to1985[i] <- mean(nodes@data$bw1975to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1989[i] <- mean(nodes@data$bw1985to1989[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1989to2000[i] <- mean(nodes@data$bw1989to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2006[i] <- mean(nodes@data$bw2000to2006[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2006to2014[i] <- mean(nodes@data$bw2006to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1904 #official founding wikipedia
tdat$average_age <- ((historical_anchor + ((1961 - historical_anchor) / 2)) * tdat$pre_1961 ) + 
((1961 + ((1975 - 1961) / 2)) * tdat$bw1961to1975 ) + 
((1975 + ((1985 - 1975) / 2)) * tdat$bw1975to1985 ) + 
((1985 + ((1989 - 1985) / 2)) * tdat$bw1985to1989 ) + 
((1989 + ((2000 - 1989) / 2)) * tdat$bw1989to2000 ) + 
((2000 + ((2006 - 2000) / 2)) * tdat$bw2000to2006 ) + 
((2006 + ((2014 - 2006) / 2)) * tdat$bw2006to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="nakuru-4729",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)


#pre_1961
test <- over(boulevards, t1961,  returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1961 <- 0
boulevards$pre_1961[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1961)

#bw1961to1975
test <- over(boulevards, t1975,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1961to1975 <- 0
boulevards$bw1961to1975[is.na(test$id)==F & boulevards$pre_1961 ==0] <- 1
table(boulevards$pre_1961, boulevards$bw1961to1975)


#bw1975to1985
test <- over(boulevards, t1985,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1975to1985 <- 0
boulevards$bw1975to1985[is.na(test$id)==F & boulevards$pre_1961 ==0 & boulevards$bw1961to1975 ==0] <- 1
table(boulevards$bw1961to1975, boulevards$bw1975to1985)


#bw1985to1989
test <- over(boulevards, t1989,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1989 <- 0
boulevards$bw1985to1989[is.na(test$fid)==F & boulevards$pre_1961 ==0 & boulevards$bw1961to1975 ==0 & boulevards$bw1975to1985 ==0] <- 1
table(boulevards$bw1975to1985, boulevards$bw1985to1989)


#bw1989to2000
test <- over(boulevards, t2000,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1989to2000 <- 0
boulevards$bw1989to2000[is.na(test$fid)==F & boulevards$pre_1961 ==0 & boulevards$bw1961to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1989 ==0] <- 1
table(boulevards$bw1985to1989, boulevards$bw1989to2000)

#bw2000to2006
test <- over(boulevards, t2006,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2006 <- 0
boulevards$bw2000to2006[is.na(test$id)==F & boulevards$pre_1961 ==0 & boulevards$bw1961to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1989 ==0 & boulevards$bw1989to2000 ==0] <- 1
table(boulevards$bw1989to2000, boulevards$bw2000to2006)


#bw2006to2014
test <- over(boulevards, t2014,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2006to2014 <- 0
boulevards$bw2006to2014[is.na(test$fid)==F & boulevards$pre_1961 ==0 & boulevards$bw1961to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1989 ==0 & boulevards$bw1989to2000 ==0 & boulevards$bw2000to2006 ==0] <- 1
table(boulevards$bw2000to2006, boulevards$bw2006to2014)



#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1961 ==0 & boulevards$bw1961to1975 ==0 & boulevards$bw1975to1985 ==0 & boulevards$bw1985to1989 ==0 & boulevards$bw1989to2000 ==0 & boulevards$bw2000to2006 ==0 & boulevards$bw2006to2014 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[30], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "nakuru-4729", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 30: NDOLA ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/zambia-ZMB_gpkg/ndola-3859.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/zambia-ZMB_gpkg/ndola-3859.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "ndola-3859", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "ndola-3859", ".Rdata", sep="")	)


#urban extent at 1959
t1959 <- readOGR("./extents/ndola/ndola1959.shp")
class(t1959)
proj4string(t1959)

#urban extent at 1974
t1974 <- readOGR("./extents/ndola/ndola1974.shp")
class(t1974)
proj4string(t1974)

#urban extent at 1985
t1985 <- readOGR("./extents/ndola/ndola1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1989
t1989 <- readOGR("./extents/ndola/urbFootprint_t1_subset_pruned.shp")
class(t1989)
proj4string(t1989)

#urban extent at 2002
t2002 <- readOGR("./extents/ndola/urbFootprint_t2_subset_pruned.shp")
class(t2002)
proj4string(t2002)

#urban extent at 2009
t2009 <- readOGR("./extents/ndola/ndola2009.shp")
class(t2009)
proj4string(t2009)

#urban extent at 2014
t2014 <- readOGR("./extents/ndola/urbFootprint_t3_subset_pruned.shp")
class(t2014)
proj4string(t2014)


#pre_1959
test <- over(nodes, t1959)
length(test)
dim(nodes)
head(test)
nodes$pre_1959 <- 0
nodes$pre_1959[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1959)

#bw1959to1974
test <- over(nodes, t1974)
dim(test)
dim(nodes)
head(test)
nodes$bw1959to1974 <- 0
nodes$bw1959to1974[is.na(test$id)==F & nodes$pre_1959 ==0] <- 1
table(nodes$pre_1959, nodes$bw1959to1974)


#bw1974to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1974to1985 <- 0
nodes$bw1974to1985[is.na(test$id)==F & nodes$pre_1959 ==0 & nodes$bw1959to1974 ==0] <- 1
table(nodes$bw1959to1974, nodes$bw1974to1985)


#bw1985to1989
test <- over(nodes, t1989)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1989 <- 0
nodes$bw1985to1989[is.na(test$fid)==F & nodes$pre_1959 ==0 & nodes$bw1959to1974 ==0 & nodes$bw1974to1985 ==0] <- 1
table(nodes$bw1974to1985, nodes$bw1985to1989)


#bw1989to2002
test <- over(nodes, t2002)
dim(test)
dim(nodes)
head(test)
nodes$bw1989to2002 <- 0
nodes$bw1989to2002[is.na(test$fid)==F & nodes$pre_1959 ==0 & nodes$bw1959to1974 ==0 & nodes$bw1974to1985 ==0 & nodes$bw1985to1989 ==0] <- 1
table(nodes$bw1985to1989, nodes$bw1989to2002)


#bw2002to2009
test <- over(nodes, t2009)
dim(test)
dim(nodes)
head(test)
nodes$bw2002to2009 <- 0
nodes$bw2002to2009[is.na(test$id)==F & nodes$pre_1959 ==0 & nodes$bw1959to1974 ==0 & nodes$bw1974to1985 ==0 & nodes$bw1985to1989 ==0 & nodes$bw1989to2002 ==0] <- 1
table(nodes$bw1989to2002, nodes$bw2002to2009)


#bw2009to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2009to2014 <- 0
nodes$bw2009to2014[is.na(test$fid)==F & nodes$pre_1959 ==0 & nodes$bw1959to1974 ==0 & nodes$bw1974to1985 ==0 & nodes$bw1985to1989 ==0 & nodes$bw1989to2002 ==0 & nodes$bw2002to2009 ==0] <- 1
table(nodes$bw2002to2009, nodes$bw2009to2014)


#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1959 ==0 & nodes$bw1959to1974 ==0 & nodes$bw1974to1985 ==0 & nodes$bw1985to1989 ==0 & nodes$bw1989to2002 ==0 & nodes$bw2002to2009 ==0 & nodes$bw2009to2014 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1959 <- NA
tdat$bw1959to1974 <- NA
tdat$bw1974to1985 <- NA
tdat$bw1985to1989 <- NA
tdat$bw1989to2002 <- NA
tdat$bw2002to2009 <- NA
tdat$bw2009to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1959[i] <- mean(nodes@data$pre_1959[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1959to1974[i] <- mean(nodes@data$bw1959to1974[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1974to1985[i] <- mean(nodes@data$bw1974to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1989[i] <- mean(nodes@data$bw1985to1989[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1989to2002[i] <- mean(nodes@data$bw1989to2002[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2002to2009[i] <- mean(nodes@data$bw2002to2009[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2009to2014[i] <- mean(nodes@data$bw2009to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1904 #wikipedia
tdat$average_age <- ((historical_anchor + ((1959 - historical_anchor) / 2)) * tdat$pre_1959 ) + 
((1959 + ((1974 - 1959) / 2)) * tdat$bw1959to1974 ) + 
((1974 + ((1985 - 1974) / 2)) * tdat$bw1974to1985 ) + 
((1985 + ((1989 - 1985) / 2)) * tdat$bw1985to1989 ) + 
((1989 + ((2002 - 1989) / 2)) * tdat$bw1989to2002 ) + 
((2002 + ((2009 - 2002) / 2)) * tdat$bw2002to2009 ) + 
((2009 + ((2014 - 2009) / 2)) * tdat$bw2009to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="ndola-3859",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))


nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1959
test <- over(boulevards, t1959, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1959 <- 0
boulevards$pre_1959[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1959)

#bw1959to1974
test <- over(boulevards, t1974, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1959to1974 <- 0
boulevards$bw1959to1974[is.na(test$id)==F & boulevards$pre_1959 ==0] <- 1
table(boulevards$pre_1959, boulevards$bw1959to1974)


#bw1974to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1974to1985 <- 0
boulevards$bw1974to1985[is.na(test$id)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1974 ==0] <- 1
table(boulevards$bw1959to1974, boulevards$bw1974to1985)


#bw1985to1989
test <- over(boulevards, t1989, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1989 <- 0
boulevards$bw1985to1989[is.na(test$fid)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1974 ==0 & boulevards$bw1974to1985 ==0] <- 1
table(boulevards$bw1974to1985, boulevards$bw1985to1989)


#bw1989to2002
test <- over(boulevards, t2002, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1989to2002 <- 0
boulevards$bw1989to2002[is.na(test$fid)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1974 ==0 & boulevards$bw1974to1985 ==0 & boulevards$bw1985to1989 ==0] <- 1
table(boulevards$bw1985to1989, boulevards$bw1989to2002)


#bw2002to2009
test <- over(boulevards, t2009, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2002to2009 <- 0
boulevards$bw2002to2009[is.na(test$id)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1974 ==0 & boulevards$bw1974to1985 ==0 & boulevards$bw1985to1989 ==0 & boulevards$bw1989to2002 ==0] <- 1
table(boulevards$bw1989to2002, boulevards$bw2002to2009)


#bw2009to2014
test <- over(boulevards, t2014, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2009to2014 <- 0
boulevards$bw2009to2014[is.na(test$fid)==F & boulevards$pre_1959 ==0 & boulevards$bw1959to1974 ==0 & boulevards$bw1974to1985 ==0 & boulevards$bw1985to1989 ==0 & boulevards$bw1989to2002 ==0 & boulevards$bw2002to2009 ==0] <- 1
table(boulevards$bw2002to2009, boulevards$bw2009to2014)


#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1959 ==0 & boulevards$bw1959to1974 ==0 & boulevards$bw1974to1985 ==0 & boulevards$bw1985to1989 ==0 & boulevards$bw1989to2002 ==0 & boulevards$bw2002to2009 ==0 & boulevards$bw2009to2014 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[31], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km




save(tdat, file=paste("./boeing_data/tcommunities/", "ndola-3859", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 31: OYO ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/oyo-2191.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/nigeria-NGA_gpkg/oyo-2191.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "oyo-2191", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "oyo-2191", ".Rdata", sep="")	)


#urban extent at 1917
t1917 <- readOGR("./extents/oyo/oyo1917.shp")
class(t1917)
proj4string(t1917)

#urban extent at 1985
t1985 <- readOGR("./extents/oyo/oyo1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1990
t1990 <- readOGR("./extents/oyo/urbFootprint_t1_subset_pruned.shp")
class(t1990)
proj4string(t1990)

#urban extent at 2000
t2000 <- readOGR("./extents/oyo/urbFootprint_t2_subset_pruned.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2007
t2007 <- readOGR("./extents/oyo/oyo2007.shp")
class(t2007)
proj4string(t2007)

#urban extent at 2014
t2014 <- readOGR("./extents/oyo/urbFootprint_t3_subset_pruned.shp")
class(t2014)
proj4string(t2014)


#pre_1917
test <- over(nodes, t1917)
length(test)
dim(nodes)
head(test)
nodes$pre_1917 <- 0
nodes$pre_1917[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1917)

#bw1917to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1917to1985 <- 0
nodes$bw1917to1985[is.na(test$id)==F & nodes$pre_1917 ==0] <- 1
table(nodes$pre_1917, nodes$bw1917to1985)

#bw1985to1990
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1990 <- 0
nodes$bw1985to1990[is.na(test$fid)==F & nodes$pre_1917 ==0 & nodes$bw1917to1985 ==0] <- 1
table(nodes$bw1917to1985, nodes$bw1985to1990)

#bw1990to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to2000 <- 0
nodes$bw1990to2000[is.na(test$fid)==F & nodes$pre_1917 ==0 & nodes$bw1917to1985 ==0 & nodes$bw1985to1990 ==0] <- 1
table(nodes$bw1985to1990, nodes$bw1990to2000)

#bw2000to2007
test <- over(nodes, t2007)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2007 <- 0
nodes$bw2000to2007[is.na(test$id)==F & nodes$pre_1917 ==0 & nodes$bw1917to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to2000 ==0] <- 1
table(nodes$bw1990to2000, nodes$bw2000to2007)


#bw2007to2014
test <- over(nodes, t2014)
dim(test)
dim(nodes)
head(test)
nodes$bw2007to2014 <- 0
nodes$bw2007to2014[is.na(test$fid)==F & nodes$pre_1917 ==0 & nodes$bw1917to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to2000 ==0 & nodes$bw2000to2007 ==0] <- 1
table(nodes$bw2000to2007, nodes$bw2007to2014)


#bw2014to2020
nodes$bw2014to2020 <- 0
nodes$bw2014to2020[ nodes$pre_1917 ==0 & nodes$bw1917to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to2000 ==0 & nodes$bw2000to2007 ==0 & nodes$bw2007to2014 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1917 <- NA
tdat$bw1917to1985 <- NA
tdat$bw1985to1990 <- NA
tdat$bw1990to2000 <- NA
tdat$bw2000to2007 <- NA
tdat$bw2007to2014 <- NA
tdat$bw2014to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1917[i] <- mean(nodes@data$pre_1917[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1917to1985[i] <- mean(nodes@data$bw1917to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1990[i] <- mean(nodes@data$bw1985to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to2000[i] <- mean(nodes@data$bw1990to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2007[i] <- mean(nodes@data$bw2000to2007[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2007to2014[i] <- mean(nodes@data$bw2007to2014[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2014to2020[i] <- mean(nodes@data$bw2014to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1835 
tdat$average_age <- ((historical_anchor + ((1917 - historical_anchor) / 2)) * tdat$pre_1917 ) + 
((1917 + ((1985 - 1917) / 2)) * tdat$bw1917to1985 ) + 
((1985 + ((1990 - 1985) / 2)) * tdat$bw1985to1990 ) + 
((1990 + ((2000 - 1990) / 2)) * tdat$bw1990to2000 ) + 
((2000 + ((2007 - 2000) / 2)) * tdat$bw2000to2007 ) + 
((2007 + ((2014 - 2007) / 2)) * tdat$bw2007to2014 ) + 
((2014 + ((2020 - 2014) / 2)) * tdat$bw2014to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="oyo-2191",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link", "['motorway_link', 'motorway']"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1917
test <- over(boulevards, t1917, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1917 <- 0
boulevards$pre_1917[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1917)

#bw1917to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1917to1985 <- 0
boulevards$bw1917to1985[is.na(test$id)==F & boulevards$pre_1917 ==0] <- 1
table(boulevards$pre_1917, boulevards$bw1917to1985)

#bw1985to1990
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1990 <- 0
boulevards$bw1985to1990[is.na(test$fid)==F & boulevards$pre_1917 ==0 & boulevards$bw1917to1985 ==0] <- 1
table(boulevards$bw1917to1985, boulevards$bw1985to1990)

#bw1990to2000
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to2000 <- 0
boulevards$bw1990to2000[is.na(test$fid)==F & boulevards$pre_1917 ==0 & boulevards$bw1917to1985 ==0 & boulevards$bw1985to1990 ==0] <- 1
table(boulevards$bw1985to1990, boulevards$bw1990to2000)

#bw2000to2007
test <- over(boulevards, t2007, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2007 <- 0
boulevards$bw2000to2007[is.na(test$id)==F & boulevards$pre_1917 ==0 & boulevards$bw1917to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to2000 ==0] <- 1
table(boulevards$bw1990to2000, boulevards$bw2000to2007)


#bw2007to2014
test <- over(boulevards, t2014, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2007to2014 <- 0
boulevards$bw2007to2014[is.na(test$fid)==F & boulevards$pre_1917 ==0 & boulevards$bw1917to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to2000 ==0 & boulevards$bw2000to2007 ==0] <- 1
table(boulevards$bw2000to2007, boulevards$bw2007to2014)


#bw2014to2020
boulevards$bw2014to2020 <- 0
boulevards$bw2014to2020[ boulevards$pre_1917 ==0 & boulevards$bw1917to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to2000 ==0 & boulevards$bw2000to2007 ==0 & boulevards$bw2007to2014 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[32], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km




save(tdat, file=paste("./boeing_data/tcommunities/", "oyo-2191", "_tdat_wyears_NEW", ".Rdata", sep="")	)


#####################################
##### CITY 32: POINTE NOIRE ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/republic_of_congo-COG_gpkg/pointe_noire-2982.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/republic_of_congo-COG_gpkg/pointe_noire-2982.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "pointe_noire-2982", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "pointe_noire-2982", ".Rdata", sep="")	)


#urban extent at 1925
t1925 <- readOGR("./extents/pointe_noire/pointe_noire1925.shp")
class(t1925)
proj4string(t1925)

#urban extent at 1971
t1971 <- readOGR("./extents/pointe_noire/pointe_noire1971.shp")
class(t1971)
proj4string(t1971)

#urban extent at 1984
t1984 <- readOGR("./extents/pointe_noire/pointe_noire1984.shp")
class(t1984)
proj4string(t1984)

#urban extent at 1992
t1992 <- readOGR("./extents/pointe_noire/pointe_noire1992.shp")
class(t1992)
proj4string(t1992)

#urban extent at 2000
t2000 <- readOGR("./extents/pointe_noire/pointe_noire2000.shp")
class(t2000)
proj4string(t2000)

#urban extent at 2011
t2011 <- readOGR("./extents/pointe_noire/pointe_noire2011.shp")
class(t2011)
proj4string(t2011)

#pre_1925
test <- over(nodes, t1925)
length(test)
dim(nodes)
head(test)
nodes$pre_1925 <- 0
nodes$pre_1925[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1925)

#bw1925to1971
test <- over(nodes, t1971)
dim(test)
dim(nodes)
head(test)
nodes$bw1925to1971 <- 0
nodes$bw1925to1971[is.na(test$id)==F & nodes$pre_1925 ==0] <- 1
table(nodes$pre_1925, nodes$bw1925to1971)


#bw1971to1984
test <- over(nodes, t1984)
dim(test)
dim(nodes)
head(test)
nodes$bw1971to1984 <- 0
nodes$bw1971to1984[is.na(test$id)==F & nodes$pre_1925 ==0 & nodes$bw1925to1971 ==0] <- 1
table(nodes$bw1925to1971, nodes$bw1971to1984)


#bw1984to1992
test <- over(nodes, t1992)
dim(test)
dim(nodes)
head(test)
nodes$bw1984to1992 <- 0
nodes$bw1984to1992[is.na(test$id)==F & nodes$pre_1925 ==0 & nodes$bw1925to1971 ==0 & nodes$bw1971to1984 ==0] <- 1
table(nodes$bw1971to1984, nodes$bw1984to1992)


#bw1992to2000
test <- over(nodes, t2000)
dim(test)
dim(nodes)
head(test)
nodes$bw1992to2000 <- 0
nodes$bw1992to2000[is.na(test$id)==F & nodes$pre_1925 ==0 & nodes$bw1925to1971 ==0 & nodes$bw1971to1984 ==0 & nodes$bw1984to1992 ==0] <- 1
table(nodes$bw1984to1992, nodes$bw1992to2000)


#bw2000to2011
test <- over(nodes, t2011)
dim(test)
dim(nodes)
head(test)
nodes$bw2000to2011 <- 0
nodes$bw2000to2011[is.na(test$id)==F & nodes$pre_1925 ==0 & nodes$bw1925to1971 ==0 & nodes$bw1971to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2000 ==0] <- 1
table(nodes$bw1992to2000, nodes$bw2000to2011)



#bw2011to2020
nodes$bw2011to2020 <- 0
nodes$bw2011to2020[ nodes$pre_1925 ==0 & nodes$bw1925to1971 ==0 & nodes$bw1971to1984 ==0 & nodes$bw1984to1992 ==0 & nodes$bw1992to2000 ==0 & nodes$bw2000to2011 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1925 <- NA
tdat$bw1925to1971 <- NA
tdat$bw1971to1984 <- NA
tdat$bw1984to1992 <- NA
tdat$bw1992to2000 <- NA
tdat$bw2000to2011 <- NA
tdat$bw2011to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1925[i] <- mean(nodes@data$pre_1925[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1925to1971[i] <- mean(nodes@data$bw1925to1971[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1971to1984[i] <- mean(nodes@data$bw1971to1984[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1984to1992[i] <- mean(nodes@data$bw1984to1992[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1992to2000[i] <- mean(nodes@data$bw1992to2000[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2000to2011[i] <- mean(nodes@data$bw2000to2011[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2011to2020[i] <- mean(nodes@data$bw2011to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1883 #see: wikipedia
tdat$average_age <- ((historical_anchor + ((1925 - historical_anchor) / 2)) * tdat$pre_1925 ) + 
((1925 + ((1971 - 1925) / 2)) * tdat$bw1925to1971 ) + 
((1971 + ((1984 - 1971) / 2)) * tdat$bw1971to1984 ) + 
((1984 + ((1992 - 1984) / 2)) * tdat$bw1984to1992 ) + 
((1992 + ((2000 - 1992) / 2)) * tdat$bw1992to2000 ) + 
((2000 + ((2011 - 2000) / 2)) * tdat$bw2000to2011 ) + 
((2011 + ((2020 - 2011) / 2)) * tdat$bw2011to2020 ) 
summary(tdat$average_age)


#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="pointe_noire-2982",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1925
test <- over(boulevards, t1925, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1925 <- 0
boulevards$pre_1925[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1925)

#bw1925to1971
test <- over(boulevards, t1971, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1925to1971 <- 0
boulevards$bw1925to1971[is.na(test$id)==F & boulevards$pre_1925 ==0] <- 1
table(boulevards$pre_1925, boulevards$bw1925to1971)


#bw1971to1984
test <- over(boulevards, t1984, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1971to1984 <- 0
boulevards$bw1971to1984[is.na(test$id)==F & boulevards$pre_1925 ==0 & boulevards$bw1925to1971 ==0] <- 1
table(boulevards$bw1925to1971, boulevards$bw1971to1984)


#bw1984to1992
test <- over(boulevards, t1992, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1984to1992 <- 0
boulevards$bw1984to1992[is.na(test$id)==F & boulevards$pre_1925 ==0 & boulevards$bw1925to1971 ==0 & boulevards$bw1971to1984 ==0] <- 1
table(boulevards$bw1971to1984, boulevards$bw1984to1992)


#bw1992to2000
test <- over(boulevards, t2000, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1992to2000 <- 0
boulevards$bw1992to2000[is.na(test$id)==F & boulevards$pre_1925 ==0 & boulevards$bw1925to1971 ==0 & boulevards$bw1971to1984 ==0 & boulevards$bw1984to1992 ==0] <- 1
table(boulevards$bw1984to1992, boulevards$bw1992to2000)


#bw2000to2011
test <- over(boulevards, t2011, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2000to2011 <- 0
boulevards$bw2000to2011[is.na(test$id)==F & boulevards$pre_1925 ==0 & boulevards$bw1925to1971 ==0 & boulevards$bw1971to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2000 ==0] <- 1
table(boulevards$bw1992to2000, boulevards$bw2000to2011)

#bw2011to2020
boulevards$bw2011to2020 <- 0
boulevards$bw2011to2020[ boulevards$pre_1925 ==0 & boulevards$bw1925to1971 ==0 & boulevards$bw1971to1984 ==0 & boulevards$bw1984to1992 ==0 & boulevards$bw1992to2000 ==0 & boulevards$bw2000to2011 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[33], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km



save(tdat, file=paste("./boeing_data/tcommunities/", "pointe_noire-2982", "_tdat_wyears_NEW", ".Rdata", sep="")	)




#####################################
##### CITY 33: PORT ELIZABETH ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/south_africa-ZAF_gpkg/port_elizabeth-3505.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/south_africa-ZAF_gpkg/port_elizabeth-3505.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "port_elizabeth-3505", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "port_elizabeth-3505", ".Rdata", sep="")	)

#urban extent at 1922
t1922 <- readOGR("./extents/port_elizabeth/port_elizabeth1922.shp")
class(t1922)
proj4string(t1922)

#urban extent at 1947
t1947 <- readOGR("./extents/port_elizabeth/port_elizabeth1947.shp")
class(t1947)
proj4string(t1947)

#urban extent at 1963
t1963 <- readOGR("./extents/port_elizabeth/port_elizabeth1963.shp")
class(t1963)
proj4string(t1963)


#urban extent at 1984
t1984 <- readOGR("./extents/port_elizabeth/port_elizabeth1984.shp")
class(t1984)
proj4string(t1984)


#urban extent at 1990
t1990 <- readOGR("./extents/port_elizabeth/urbFootprint_t1_subset_pruned.shp")
class(t1990)
proj4string(t1990)


#urban extent at 2001
t2001 <- readOGR("./extents/port_elizabeth/urbFootprint_t2_subset_pruned.shp")
class(t2001)
proj4string(t2001)

#urban extent at 2006
t2006 <- readOGR("./extents/port_elizabeth/port_elizabeth2006.shp")
class(t2006)
proj4string(t2006)

#urban extent at 2013
t2013 <- readOGR("./extents/port_elizabeth/urbFootprint_t3_subset_pruned.shp")
class(t2013)
proj4string(t2013)


#pre_1922
test <- over(nodes, t1922)
length(test)
dim(nodes)
head(test)
nodes$pre_1922 <- 0
nodes$pre_1922[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1922)

#bw1922to1947
test <- over(nodes, t1947)
dim(test)
dim(nodes)
head(test)
nodes$bw1922to1947 <- 0
nodes$bw1922to1947[is.na(test$id)==F & nodes$pre_1922 ==0] <- 1
table(nodes$pre_1922, nodes$bw1922to1947)


#bw1947to1963
test <- over(nodes, t1963)
dim(test)
dim(nodes)
head(test)
nodes$bw1947to1963 <- 0
nodes$bw1947to1963[is.na(test$id)==F & nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0] <- 1
table(nodes$bw1922to1947, nodes$bw1947to1963)


#bw1963to1984
test <- over(nodes, t1984)
dim(test)
dim(nodes)
head(test)
nodes$bw1963to1984 <- 0
nodes$bw1963to1984[is.na(test$id)==F & nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0 & nodes$bw1947to1963 ==0] <- 1
table(nodes$bw1947to1963, nodes$bw1963to1984)


#bw1984to1990
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1984to1990 <- 0
nodes$bw1984to1990[is.na(test$fid)==F & nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0 & nodes$bw1947to1963 ==0 & nodes$bw1963to1984 ==0] <- 1
table(nodes$bw1963to1984, nodes$bw1984to1990)


#bw1990to2001
test <- over(nodes, t2001)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to2001 <- 0
nodes$bw1990to2001[is.na(test$fid)==F & nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0 & nodes$bw1947to1963 ==0 & nodes$bw1963to1984 ==0 & nodes$bw1984to1990 ==0] <- 1
table(nodes$bw1984to1990, nodes$bw1990to2001)


#bw2001to2006
test <- over(nodes, t2006)
dim(test)
dim(nodes)
head(test)
nodes$bw2001to2006 <- 0
nodes$bw2001to2006[is.na(test$id)==F & nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0 & nodes$bw1947to1963 ==0 & nodes$bw1963to1984 ==0 & nodes$bw1984to1990 ==0 & nodes$bw1990to2001 ==0] <- 1
table(nodes$bw1990to2001, nodes$bw2001to2006)


#bw2006to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw2006to2013 <- 0
nodes$bw2006to2013[is.na(test$fid)==F & nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0 & nodes$bw1947to1963 ==0 & nodes$bw1963to1984 ==0 & nodes$bw1984to1990 ==0 & nodes$bw1990to2001 ==0 & nodes$bw2001to2006 ==0] <- 1
table(nodes$bw2001to2006, nodes$bw2006to2013)


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1922 ==0 & nodes$bw1922to1947 ==0 & nodes$bw1947to1963 ==0 & nodes$bw1963to1984 ==0 & nodes$bw1984to1990 ==0 & nodes$bw1990to2001 ==0 & nodes$bw2001to2006 ==0 & nodes$bw2006to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1922 <- NA
tdat$bw1922to1947 <- NA
tdat$bw1947to1963 <- NA
tdat$bw1963to1984 <- NA
tdat$bw1984to1990 <- NA
tdat$bw1990to2001 <- NA
tdat$bw2001to2006 <- NA
tdat$bw2006to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1922[i] <- mean(nodes@data$pre_1922[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1922to1947[i] <- mean(nodes@data$bw1922to1947[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1947to1963[i] <- mean(nodes@data$bw1947to1963[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1963to1984[i] <- mean(nodes@data$bw1963to1984[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1984to1990[i] <- mean(nodes@data$bw1984to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to2001[i] <- mean(nodes@data$bw1990to2001[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2001to2006[i] <- mean(nodes@data$bw2001to2006[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2006to2013[i] <- mean(nodes@data$bw2006to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1820 #first large settlement of british soldiers that lead to building of the town
tdat$average_age <- ((historical_anchor + ((1922 - historical_anchor) / 2)) * tdat$pre_1922 ) + 
((1922 + ((1947 - 1922) / 2)) * tdat$bw1922to1947 ) + 
((1947 + ((1963 - 1947) / 2)) * tdat$bw1947to1963 ) + 
((1963 + ((1984 - 1963) / 2)) * tdat$bw1963to1984 ) + 
((1984 + ((1990 - 1984) / 2)) * tdat$bw1984to1990 ) + 
((1990 + ((2001 - 1990) / 2)) * tdat$bw1990to2001 ) + 
((2001 + ((2006 - 2001) / 2)) * tdat$bw2001to2006 ) + 
((2006 + ((2013 - 2006) / 2)) * tdat$bw2006to2013 ) +
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="port_elizabeth-3505",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)



#pre_1922
test <- over(boulevards, t1922, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1922 <- 0
boulevards$pre_1922[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1922)

#bw1922to1947
test <- over(boulevards, t1947, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1922to1947 <- 0
boulevards$bw1922to1947[is.na(test$id)==F & boulevards$pre_1922 ==0] <- 1
table(boulevards$pre_1922, boulevards$bw1922to1947)


#bw1947to1963
test <- over(boulevards, t1963, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1947to1963 <- 0
boulevards$bw1947to1963[is.na(test$id)==F & boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0] <- 1
table(boulevards$bw1922to1947, boulevards$bw1947to1963)


#bw1963to1984
test <- over(boulevards, t1984, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1963to1984 <- 0
boulevards$bw1963to1984[is.na(test$id)==F & boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0 & boulevards$bw1947to1963 ==0] <- 1
table(boulevards$bw1947to1963, boulevards$bw1963to1984)


#bw1984to1990
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1984to1990 <- 0
boulevards$bw1984to1990[is.na(test$fid)==F & boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0 & boulevards$bw1947to1963 ==0 & boulevards$bw1963to1984 ==0] <- 1
table(boulevards$bw1963to1984, boulevards$bw1984to1990)


#bw1990to2001
test <- over(boulevards, t2001, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to2001 <- 0
boulevards$bw1990to2001[is.na(test$fid)==F & boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0 & boulevards$bw1947to1963 ==0 & boulevards$bw1963to1984 ==0 & boulevards$bw1984to1990 ==0] <- 1
table(boulevards$bw1984to1990, boulevards$bw1990to2001)


#bw2001to2006
test <- over(boulevards, t2006, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2001to2006 <- 0
boulevards$bw2001to2006[is.na(test$id)==F & boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0 & boulevards$bw1947to1963 ==0 & boulevards$bw1963to1984 ==0 & boulevards$bw1984to1990 ==0 & boulevards$bw1990to2001 ==0] <- 1
table(boulevards$bw1990to2001, boulevards$bw2001to2006)


#bw2006to2013
test <- over(boulevards, t2013, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2006to2013 <- 0
boulevards$bw2006to2013[is.na(test$fid)==F & boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0 & boulevards$bw1947to1963 ==0 & boulevards$bw1963to1984 ==0 & boulevards$bw1984to1990 ==0 & boulevards$bw1990to2001 ==0 & boulevards$bw2001to2006 ==0] <- 1
table(boulevards$bw2001to2006, boulevards$bw2006to2013)


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[ boulevards$pre_1922 ==0 & boulevards$bw1922to1947 ==0 & boulevards$bw1947to1963 ==0 & boulevards$bw1963to1984 ==0 & boulevards$bw1984to1990 ==0 & boulevards$bw1990to2001 ==0 & boulevards$bw2001to2006 ==0 & boulevards$bw2006to2013 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[34], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard", "ref")]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "port_elizabeth-3505", "_tdat_wyears_NEW", ".Rdata", sep="")	)

#####################################
##### CITY 34: PORTO NOVO ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/benin-BEN_gpkg/porto_novo-2101.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/benin-BEN_gpkg/porto_novo-2101.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "porto_novo-2101", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "porto_novo-2101", ".Rdata", sep="")	)


#urban extent at 1930
t1930 <- readOGR("./extents/porto_novo/porto_novo_1930.shp")
class(t1930)
proj4string(t1930)

#urban extent at 1955
t1955 <- readOGR("./extents/porto_novo/porto_novo_1955.shp")
class(t1955)
proj4string(t1955)

#urban extent at 1971
t1971 <- readOGR("./extents/porto_novo/porto_novo1971.shp")
class(t1971)
proj4string(t1971)

#urban extent at 1980
t1980 <- readOGR("./extents/porto_novo/porto_novo1980.shp")
class(t1980)
proj4string(t1980)

#urban extent at 1992
t1992 <- readOGR("./extents/porto_novo/porto_novo1992.shp")
class(t1992)
proj4string(t1992)

#urban extent at 2013
t2013 <- readOGR("./extents/porto_novo/porto_novo2013.shp")
class(t2013)
proj4string(t2013)


#pre_1930
test <- over(nodes, t1930)
length(test)
dim(nodes)
head(test)
nodes$pre_1930 <- 0
nodes$pre_1930[is.na(test$name)==F] <- 1
table(test$name)
table(nodes$pre_1930)


#bw1930to1955
test <- over(nodes, t1955)
dim(test)
dim(nodes)
head(test)
nodes$bw1930to1955 <- 0
nodes$bw1930to1955[is.na(test$name)==F & nodes$pre_1930 ==0] <- 1
table(nodes$pre_1930, nodes$bw1930to1955)

#bw1955to1971
test <- over(nodes, t1971)
dim(test)
dim(nodes)
head(test)
nodes$bw1955to1971 <- 0
nodes$bw1955to1971[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1955 ==0] <- 1
table(nodes$bw1930to1955, nodes$bw1955to1971)

#bw1971to1980
test <- over(nodes, t1980)
dim(test)
dim(nodes)
head(test)
nodes$bw1971to1980 <- 0
nodes$bw1971to1980[is.na(test$id)==F  & nodes$pre_1930 ==0 & nodes$bw1930to1955 ==0 & nodes$bw1955to1971 ==0 ] <- 1
table(nodes$bw1955to1971, nodes$bw1971to1980)

#bw1980to1992
test <- over(nodes, t1992)
dim(test)
dim(nodes)
head(test)
nodes$bw1980to1992 <- 0
nodes$bw1980to1992[is.na(test$id)==F & nodes$pre_1930 ==0 & nodes$bw1930to1955 ==0 & nodes$bw1955to1971 ==0 & nodes$bw1971to1980 ==0] <- 1
table(nodes$bw1971to1980, nodes$bw1980to1992)


#bw1992to2013
test <- over(nodes, t2013)
dim(test)
dim(nodes)
head(test)
nodes$bw1992to2013 <- 0
nodes$bw1992to2013[is.na(test$id)==F  & nodes$pre_1930 ==0 & nodes$bw1930to1955 ==0 & nodes$bw1955to1971 ==0 & nodes$bw1971to1980 ==0 & nodes$bw1980to1992 ==0] <- 1
table(nodes$bw1980to1992, nodes$bw1992to2013)


#bw2013to2020
nodes$bw2013to2020 <- 0
nodes$bw2013to2020[ nodes$pre_1930 ==0 & nodes$bw1930to1955 ==0 & nodes$bw1955to1971 ==0 & nodes$bw1971to1980 ==0 & nodes$bw1980to1992 ==0 & nodes$bw1992to2013 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1930 <- NA
tdat$bw1930to1955 <- NA 
tdat$bw1955to1971 <- NA
tdat$bw1971to1980 <- NA
tdat$bw1980to1992 <- NA
tdat$bw1992to2013 <- NA
tdat$bw2013to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1930[i] <- mean(nodes@data$pre_1930[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1930to1955[i] <- mean(nodes@data$bw1930to1955[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1955to1971[i] <- mean(nodes@data$bw1955to1971[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1971to1980[i] <- mean(nodes@data$bw1971to1980[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1980to1992[i] <- mean(nodes@data$bw1980to1992[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1992to2013[i] <- mean(nodes@data$bw1992to2013[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2013to2020[i] <- mean(nodes@data$bw2013to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1730 #see wikipedia
tdat$average_age <- ((historical_anchor + ((1930 - historical_anchor) / 2)) * tdat$pre_1930 ) + 
((1930 + ((1955 - 1930) / 2)) * tdat$bw1930to1955 ) + 
((1955 + ((1971 - 1955) / 2)) * tdat$bw1955to1971 ) + 
((1971 + ((1980 - 1971) / 2)) * tdat$bw1971to1980 ) + 
((1980 + ((1992 - 1980) / 2)) * tdat$bw1980to1992 ) + 
((1992 + ((2013 - 1992) / 2)) * tdat$bw1992to2013 ) + 
((2013 + ((2020 - 2013) / 2)) * tdat$bw2013to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="porto_novo-2101",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)

table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)

#pre_1930
test <- over(boulevards, t1930)
length(test)
dim(nodes)
head(test)
boulevards $pre_1930 <- 0
boulevards $pre_1930[is.na(test$name)==F] <- 1
table(test$name)
table(boulevards $pre_1930)

#bw1930to1955
test <- over(boulevards, t1955)
dim(test)
dim(boulevards)
head(test)
boulevards $bw1930to1955 <- 0
boulevards $bw1930to1955[is.na(test$name)==F & boulevards $pre_1930 ==0] <- 1
table(boulevards $pre_1930, boulevards $bw1930to1955)

#bw1955to1971
test <- over(boulevards, t1971)
dim(test)
dim(nodes)
head(test)
boulevards $bw1955to1971 <- 0
boulevards $bw1955to1971[is.na(test$id)==F & boulevards $pre_1930 ==0 & boulevards $bw1930to1955 ==0] <- 1
table(boulevards $bw1930to1955, boulevards $bw1955to1971)

#bw1971to1980
test <- over(boulevards, t1980,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1971to1980 <- 0
boulevards$bw1971to1980[is.na(test$id)==F & boulevards $pre_1930 ==0 & boulevards $bw1930to1955 ==0 & boulevards$bw1955to1971 ==0] <- 1
table(boulevards$bw1955to1971, boulevards$bw1971to1980)

#bw1980to1992
test <- over(boulevards, t1992,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1980to1992 <- 0
boulevards$bw1980to1992[is.na(test$id)==F  & boulevards $pre_1930 ==0 & boulevards $bw1930to1955 ==0 & boulevards$bw1955to1971 ==0 & boulevards$bw1971to1980 ==0] <- 1
table(boulevards$bw1971to1980, boulevards$bw1980to1992)


#bw1992to2013
test <- over(boulevards, t2013,  returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1992to2013 <- 0
boulevards$bw1992to2013[is.na(test$id)==F  & boulevards $pre_1930 ==0 & boulevards $bw1930to1955 ==0 & boulevards$bw1955to1971 ==0 & boulevards$bw1971to1980 ==0 & boulevards$bw1980to1992 ==0] <- 1
table(boulevards$bw1980to1992, boulevards$bw1992to2013)


#bw2013to2020
boulevards$bw2013to2020 <- 0
boulevards$bw2013to2020[  boulevards $pre_1930 ==0 & boulevards $bw1930to1955 ==0 & boulevards$bw1955to1971 ==0& boulevards$bw1971to1980 ==0 & boulevards$bw1980to1992 ==0 & boulevards$bw1992to2013 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[35], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "porto_novo-2101", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 35: TOUBA ######

rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/senegal-SEN_gpkg/touba-1473.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/senegal-SEN_gpkg/touba-1473.gpkg", "edges")
class(edges)


load(paste("./boeing_data/tcommunities/", "touba-1473", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "touba-1473", ".Rdata", sep="")	)


#urban extent at 1965
t1965 <- readOGR("./extents/touba/touba1965.shp")
class(t1965)
proj4string(t1965)

#urban extent at 1979
t1979 <- readOGR("./extents/touba/touba1979.shp")
class(t1979)
proj4string(t1979)

#urban extent at 1989
t1989 <- readOGR("./extents/touba/touba1989.shp")
class(t1989)
proj4string(t1989)

#urban extent at 2001
t2001 <- readOGR("./extents/touba/touba2001.shp")
class(t2001)
proj4string(t2001)

#urban extent at 2009
t2009 <- readOGR("./extents/touba/touba2009.shp")
class(t2009)
proj4string(t2009)

#urban extent at 2015
t2015 <- readOGR("./extents/touba/touba2015.shp")
class(t2015)
proj4string(t2015)


#pre_1965
test <- over(nodes, t1965)
dim(test)
dim(nodes)
head(test)
nodes$pre_1965 <- 0
nodes$pre_1965[is.na(test$name)==F] <- 1
table(test$name)
table(nodes$pre_1965)

#bw1965to1979
test <- over(nodes, t1979)
dim(test)
dim(nodes)
head(test)
nodes$bw1965to1979 <- 0
nodes$bw1965to1979[is.na(test$name)==F & nodes$pre_1965 ==0] <- 1
table(nodes$pre_1965, nodes$bw1965to1979)

#bw1979to1989
test <- over(nodes, t1989)
dim(test)
dim(nodes)
head(test)
nodes$bw1979to1989 <- 0
nodes$bw1979to1989[is.na(test$id)==F & nodes$pre_1965 ==0 & nodes$bw1965to1979 ==0] <- 1
table(nodes$bw1965to1979, nodes$bw1979to1989)

#bw1989to2001
test <- over(nodes, t2001)
dim(test)
dim(nodes)
head(test)
nodes$bw1989to2001 <- 0
nodes$bw1989to2001[is.na(test$id)==F & nodes$pre_1965 ==0 & nodes$bw1965to1979 ==0 & nodes$bw1979to1989 ==0] <- 1
table(nodes$bw1979to1989, nodes$bw1989to2001)


#bw2001to2009
test <- over(nodes, t2009)
dim(test)
dim(nodes)
head(test)
nodes$bw2001to2009 <- 0
nodes$bw2001to2009[is.na(test$id)==F & nodes$pre_1965 ==0 & nodes$bw1965to1979 ==0 & nodes$bw1979to1989 ==0 & nodes$bw1989to2001 ==0] <- 1
table(nodes$bw1989to2001, nodes$bw2001to2009)


#bw2009to2015
test <- over(nodes, t2015)
dim(test)
dim(nodes)
head(test)
nodes$bw2009to2015 <- 0
nodes$bw2009to2015[is.na(test$id)==F& nodes$pre_1965 ==0 & nodes$bw1965to1979 ==0 & nodes$bw1979to1989 ==0 & nodes$bw1989to2001 ==0 & nodes$bw2001to2009 ==0] <- 1
table(nodes$bw2001to2009, nodes$bw2009to2015)


#bw2015to2020
nodes$bw2015to2020 <- 0
nodes$bw2015to2020[ nodes$pre_1965 ==0 & nodes$bw1965to1979 ==0 & nodes$bw1979to1989 ==0 & nodes$bw1989to2001 ==0 & nodes$bw2001to2009 ==0 & nodes$bw2009to2015 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1965 <- NA
tdat$bw1965to1979 <- NA
tdat$bw1979to1989 <- NA
tdat$bw1989to2001 <- NA
tdat$bw2001to2009 <- NA
tdat$bw2009to2015 <- NA
tdat$bw2015to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1965[i] <- mean(nodes@data$pre_1965[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1965to1979[i] <- mean(nodes@data$bw1965to1979[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1979to1989[i] <- mean(nodes@data$bw1979to1989[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1989to2001[i] <- mean(nodes@data$bw1989to2001[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2001to2009[i] <- mean(nodes@data$bw2001to2009[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2009to2015[i] <- mean(nodes@data$bw2009to2015[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2015to2020[i] <- mean(nodes@data$bw2015to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1887 #see: wikipedia (some debate about whether it should really be 1796)
tdat$average_age <- ((historical_anchor + ((1965 - historical_anchor) / 2)) * tdat$pre_1965 ) + 
((1965 + ((1979 - 1965) / 2)) * tdat$bw1965to1979 ) + 
((1979 + ((1989 - 1979) / 2)) * tdat$bw1979to1989 ) + 
((1989 + ((2001 - 1989) / 2)) * tdat$bw1989to2001 ) + 
((2001 + ((2009 - 2001) / 2)) * tdat$bw2001to2009 ) + 
((2009 + ((2015 - 2009) / 2)) * tdat$bw2009to2015 ) + 
((2015 + ((2020 - 2015) / 2)) * tdat$bw2015to2020 )
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="touba-1473",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))

nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1965
test <- over(boulevards, t1965)
dim(test)
dim(boulevards)
head(test)
boulevards$pre_1965 <- 0
boulevards$pre_1965[is.na(test$name)==F] <- 1
table(test$name)
table(boulevards$pre_1965)

#bw1965to1979
test <- over(boulevards, t1979)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1965to1979 <- 0
boulevards$bw1965to1979[is.na(test$name)==F & boulevards$pre_1965 ==0] <- 1
table(boulevards$pre_1965, boulevards$bw1965to1979)

#bw1979to1989
test <- over(boulevards, t1989)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1979to1989 <- 0
boulevards$bw1979to1989[is.na(test$id)==F & boulevards$pre_1965 ==0 & boulevards$bw1965to1979 ==0] <- 1
table(boulevards$bw1965to1979, boulevards$bw1979to1989)

#bw1989to2001
test <- over(boulevards, t2001)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1989to2001 <- 0
boulevards$bw1989to2001[is.na(test$id)==F & boulevards$pre_1965 ==0 & boulevards$bw1965to1979 ==0 & boulevards$bw1979to1989 ==0] <- 1
table(boulevards$bw1979to1989, boulevards$bw1989to2001)


#bw2001to2009
test <- over(boulevards, t2009)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2001to2009 <- 0
boulevards$bw2001to2009[is.na(test$id)==F & boulevards$pre_1965 ==0 & boulevards$bw1965to1979 ==0 & boulevards$bw1979to1989 ==0 & boulevards$bw1989to2001 ==0] <- 1
table(boulevards$bw1989to2001, boulevards$bw2001to2009)


#bw2009to2015
test <- over(boulevards, t2015)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2009to2015 <- 0
boulevards$bw2009to2015[is.na(test$id)==F& boulevards$pre_1965 ==0 & boulevards$bw1965to1979 ==0 & boulevards$bw1979to1989 ==0 & boulevards$bw1989to2001 ==0 & boulevards$bw2001to2009 ==0] <- 1
table(boulevards$bw2001to2009, boulevards$bw2009to2015)


#bw2015to2020
boulevards$bw2015to2020 <- 0
boulevards$bw2015to2020[ boulevards$pre_1965 ==0 & boulevards$bw1965to1979 ==0 & boulevards$bw1979to1989 ==0 & boulevards$bw1989to2001 ==0 & boulevards$bw2001to2009 ==0 & boulevards$bw2009to2015 ==0] <- 1


#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[36], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km




save(tdat, file=paste("./boeing_data/tcommunities/", "touba-1473", "_tdat_wyears_NEW", ".Rdata", sep="")	)



#####################################
##### CITY 36: WINDHOEK ######


rm(list=ls())

nodes <- readOGR("./boeing_data/geopackage_objects/namibia-NAM_gpkg/windhoek-3260.gpkg", "nodes")
class(nodes)

edges <- readOGR("./boeing_data/geopackage_objects/namibia-NAM_gpkg/windhoek-3260.gpkg", "edges")
class(edges)

load(paste("./boeing_data/tcommunities/", "windhoek-3260", "_tdat", ".Rdata", sep=""))

load(paste("./boeing_data/tcommunities/", "windhoek-3260", ".Rdata", sep="")	)


#urban extent at 1955
t1955 <- readOGR("./historical_maps_todigitize/windhoek/windhoek1955.shp")
class(t1955)
proj4string(t1955)

#urban extent at 1973
t1973 <- readOGR("./historical_maps_todigitize/windhoek/windhoek1973.shp")
class(t1973)
proj4string(t1973)

#urban extent at 1985
t1985 <- readOGR("./historical_maps_todigitize/windhoek/windhoek1985.shp")
class(t1985)
proj4string(t1985)

#urban extent at 1990
t1990 <- readOGR("./historical_maps_todigitize/windhoek/windhoek1990.shp")
class(t1990)
proj4string(t1990)

#urban extent at 1998
t1998 <- readOGR("./historical_maps_todigitize/windhoek/windhoek1998.shp")
class(t1998)
proj4string(t1998)

#urban extent at 2004
t2004 <- readOGR("./historical_maps_todigitize/windhoek/windhoek2004.shp")
class(t2004)
proj4string(t2004)

#urban extent at 2010
t2010 <- readOGR("./historical_maps_todigitize/windhoek/windhoek2010.shp")
class(t2010)
proj4string(t2010)


#pre_1955
test <- over(nodes, t1955)
length(test)
dim(nodes)
head(test)
nodes$pre_1955 <- 0
nodes$pre_1955[is.na(test)==F] <- 1
table(test)
table(nodes$pre_1955)

#bw1955to1973
test <- over(nodes, t1973)
dim(test)
dim(nodes)
head(test)
nodes$bw1955to1973 <- 0
nodes$bw1955to1973[is.na(test$id)==F & nodes$pre_1955 ==0] <- 1
table(nodes$pre_1955, nodes$bw1955to1973)

#bw1973to1985
test <- over(nodes, t1985)
dim(test)
dim(nodes)
head(test)
nodes$bw1973to1985 <- 0
nodes$bw1973to1985[is.na(test$id)==F & nodes$pre_1955 ==0 & nodes$bw1955to1973 ==0] <- 1
table(nodes$bw1955to1973, nodes$bw1973to1985)

#bw1985to1990
test <- over(nodes, t1990)
dim(test)
dim(nodes)
head(test)
nodes$bw1985to1990 <- 0
nodes$bw1985to1990[is.na(test$id)==F & nodes$pre_1955 ==0 & nodes$bw1955to1973 ==0 & nodes$bw1973to1985 ==0] <- 1
table(nodes$bw1973to1985, nodes$bw1985to1990)

#bw1990to1998
test <- over(nodes, t1998)
dim(test)
dim(nodes)
head(test)
nodes$bw1990to1998 <- 0
nodes$bw1990to1998[is.na(test$id)==F & nodes$pre_1955 ==0 & nodes$bw1955to1973 ==0 & nodes$bw1973to1985 ==0 & nodes$bw1985to1990 ==0] <- 1
table(nodes$bw1985to1990, nodes$bw1990to1998)

#bw1998to2004
test <- over(nodes, t2004)
dim(test)
dim(nodes)
head(test)
nodes$bw1998to2004 <- 0
nodes$bw1998to2004[is.na(test$id)==F & nodes$pre_1955 ==0 & nodes$bw1955to1973 ==0 & nodes$bw1973to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0] <- 1
table(nodes$bw1990to1998, nodes$bw1998to2004)

#bw2004to2010
test <- over(nodes, t2010)
dim(test)
dim(nodes)
head(test)
nodes$bw2004to2010 <- 0
nodes$bw2004to2010[is.na(test$id)==F & nodes$pre_1955 ==0 & nodes$bw1955to1973 ==0 & nodes$bw1973to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0 & nodes$bw1998to2004 ==0] <- 1
table(nodes$bw1998to2004, nodes$bw2004to2010)



#bw2010to2020
nodes$bw2010to2020 <- 0
nodes$bw2010to2020[ nodes$pre_1955 ==0 & nodes$bw1955to1973 ==0 & nodes$bw1973to1985 ==0 & nodes$bw1985to1990 ==0 & nodes$bw1990to1998 ==0 & nodes$bw1998to2004 ==0 & nodes$bw2004to2010 ==0] <- 1


### subset by nodes for each gml.sub.list[[x]]
tdat$pre_1955 <- NA
tdat$bw1955to1973 <- NA
tdat$bw1973to1985 <- NA
tdat$bw1985to1990 <- NA
tdat$bw1990to1998 <- NA
tdat$bw1998to2004 <- NA
tdat$bw2004to2010 <- NA
tdat$bw2010to2020 <- NA

for(i in 1:nrow(tdat)){
	tdat$pre_1955[i] <- mean(nodes@data$pre_1955[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1955to1973[i] <- mean(nodes@data$bw1955to1973[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1973to1985[i] <- mean(nodes@data$bw1973to1985[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1985to1990[i] <- mean(nodes@data$bw1985to1990[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1990to1998[i] <- mean(nodes@data$bw1990to1998[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw1998to2004[i] <- mean(nodes@data$bw1998to2004[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2004to2010[i] <- mean(nodes@data$bw2004to2010[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
	tdat$bw2010to2020[i] <- mean(nodes@data$bw2010to2020[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)

}

head(tdat)
tail(tdat)

historical_anchor <- 1890 #see: wikipedia
tdat$average_age <- ((historical_anchor + ((1955 - historical_anchor) / 2)) * tdat$pre_1955 ) + 
((1955 + ((1973 - 1955) / 2)) * tdat$bw1955to1973 ) + 
((1973 + ((1985 - 1973) / 2)) * tdat$bw1973to1985 ) + 
((1985 + ((1990 - 1985) / 2)) * tdat$bw1985to1990 ) + 
((1990 + ((1998 - 1990) / 2)) * tdat$bw1990to1998 ) + 
((1998 + ((2004 - 1998) / 2)) * tdat$bw1998to2004 ) + 
((2004 + ((2010 - 2004) / 2)) * tdat$bw2004to2010 ) + 
((2010 + ((2020 - 2010) / 2)) * tdat$bw2010to2020 ) 
summary(tdat$average_age)

#do the 12 Dec 2022 distance to hq patch 
caps <- read.csv("capital_locations_timeseries.csv", header=T, stringsAsFactors=F)
caps <- caps[caps$city=="windhoek-3260",]
head(caps)
caps.pts1 <- SpatialPoints(caps[,c("cap_x1","cap_y1")], CRS(" +proj=longlat +datum=WGS84 +no_defs "))
caps.pts1.aedc <- spTransform(caps.pts1, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))


nodes.aedc <- spTransform(nodes, CRS("+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
dist1 <- gDistance(caps.pts1.aedc, nodes.aedc, byid=T)

nodes$dist_to_hq <- NA 
nodes$dist_to_hq <- dist1[,1] 
table(is.na(nodes$dist_to_hq))

tdat$avg_dist_to_hq <- NA
for(i in 1:nrow(tdat)){
tdat$avg_dist_to_hq[i] <- mean(nodes@data$dist_to_hq[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T) / 1000 #put it in km
}
head(tdat)


#subset to the major highways (not tertiary; include highways, primary, secondary)
table(edges$highway)
boulevards <- edges[edges$highway %in% c("['motorway', 'trunk']", "['primary', 'motorway']", "['trunk', 'motorway']", "['trunk', 'trunk_link']", "motorway", "motorway_link", "primary", "primary_link", "secondary", "secondary_link", "trunk", "trunk_link"),] #note: these will be somewhat different for each city
dim(boulevards)
#plot(boulevards)

#give the highways a unique identifier: 
boulevards$b_id <- 1:nrow(boulevards)


#pre_1955
test <- over(boulevards, t1955, returnList=F)
length(test)
dim(boulevards)
head(test)
boulevards$pre_1955 <- 0
boulevards$pre_1955[is.na(test)==F] <- 1
table(test)
table(boulevards$pre_1955)

#bw1955to1973
test <- over(boulevards, t1973, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1955to1973 <- 0
boulevards$bw1955to1973[is.na(test$id)==F & boulevards$pre_1955 ==0] <- 1
table(boulevards$pre_1955, boulevards$bw1955to1973)

#bw1973to1985
test <- over(boulevards, t1985, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1973to1985 <- 0
boulevards$bw1973to1985[is.na(test$id)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1973 ==0] <- 1
table(boulevards$bw1955to1973, boulevards$bw1973to1985)

#bw1985to1990
test <- over(boulevards, t1990, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1985to1990 <- 0
boulevards$bw1985to1990[is.na(test$id)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1973 ==0 & boulevards$bw1973to1985 ==0] <- 1
table(boulevards$bw1973to1985, boulevards$bw1985to1990)

#bw1990to1998
test <- over(boulevards, t1998, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1990to1998 <- 0
boulevards$bw1990to1998[is.na(test$id)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1973 ==0 & boulevards$bw1973to1985 ==0 & boulevards$bw1985to1990 ==0] <- 1
table(boulevards$bw1985to1990, boulevards$bw1990to1998)

#bw1998to2004
test <- over(boulevards, t2004, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw1998to2004 <- 0
boulevards$bw1998to2004[is.na(test$id)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1973 ==0 & boulevards$bw1973to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0] <- 1
table(boulevards$bw1990to1998, boulevards$bw1998to2004)

#bw2004to2010
test <- over(boulevards, t2010, returnList=F)
dim(test)
dim(boulevards)
head(test)
boulevards$bw2004to2010 <- 0
boulevards$bw2004to2010[is.na(test$id)==F & boulevards$pre_1955 ==0 & boulevards$bw1955to1973 ==0 & boulevards$bw1973to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0 & boulevards$bw1998to2004 ==0] <- 1
table(boulevards$bw1998to2004, boulevards$bw2004to2010)



#bw2010to2020
boulevards$bw2010to2020 <- 0
boulevards$bw2010to2020[ boulevards$pre_1955 ==0 & boulevards$bw1955to1973 ==0 & boulevards$bw1973to1985 ==0 & boulevards$bw1985to1990 ==0 & boulevards$bw1990to1998 ==0 & boulevards$bw1998to2004 ==0 & boulevards$bw2004to2010 ==0] <- 1



#load in the graphml object for this city (the original one)

graphml_labels <- c("nigeria-NGA_graphml/abuja-2544.graphml", "nigeria-NGA_graphml/abuja-2565.graphml", "ghana-GHA_graphml/accra-1910.graphml", "ethiopia-ETH_graphml/addis_ababa-5134.graphml", "tanzania-TZA_graphml/arusha-4800.graphml", "mali-MLI_graphml/bamako-1553.graphml", "mozambique-MOZ_graphml/beira-4521.graphml", "gambia-GMB_graphml/brikama-1458.graphml", "liberia-LBR_graphml/buchanan-1533.graphml", "burundi-BDI_graphml/bujumbura-4078.graphml", "south_africa-ZAF_graphml/cape_town-3268.graphml", "botswana-BWA_graphml/gaborone-3587.graphml", "nigeria-NGA_graphml/gombe-2932.graphml", "nigeria-NGA_graphml/ibadan-2189.graphml", "south_africa-ZAF_graphml/johannesburg-3673.graphml", "uganda-UGA_graphml/kampala-4427.graphml", "togo-TGO_graphml/kara-2026.graphml", "sudan-SDN_graphml/khartoum-4335.graphml", "rwanda-RWA_graphml/kigali-4172.graphml", "democratic_republic_of_the_congo-COD_graphml/kinshasa-3209.graphml", "nigeria-NGA_graphml/lagos-2125.graphml", "angola-AGO_graphml/luanda-3050.graphml", "democratic_republic_of_the_congo-COD_graphml/lubumbashi-3756.graphml", "equatorial_guinea-GNQ_graphml/malabo-2762.graphml", "swaziland-SWZ_graphml/manzini-4080.graphml", "mozambique-MOZ_graphml/maputo-4220.graphml", "lesotho-LSO_graphml/maseru-3631.graphml", "liberia-LBR_graphml/monrovia-1526.graphml", "kenya-KEN_graphml/nairobi-4808.graphml", "kenya-KEN_graphml/nakuru-4729.graphml", "zambia-ZMB_graphml/ndola-3859.graphml", "nigeria-NGA_graphml/oyo-2191.graphml", "republic_of_congo-COG_graphml/pointe_noire-2982.graphml", "south_africa-ZAF_graphml/port_elizabeth-3505.graphml", "benin-BEN_graphml/porto_novo-2101.graphml", "senegal-SEN_graphml/touba-1473.graphml", "namibia-NAM_graphml/windhoek-3260.graphml")

#you have to update the "j" entry here
gml <- read_graph(paste("./boeing_data/graphml_objects/", graphml_labels[37], sep=""), format="graphml")
class(gml)

#calculate the number of bands and their labels
bands <- names(nodes)[! names(nodes) %in% c("osmid" ,  "y"   ,  "x"  ,"street_count" ,   "elevation" , "elevation_aster", "elevation_srtm" , "highway" , "dist_to_hq" , "dist_to_boulevard" )]

#make the place to store the resulting data:
nodes$dist_to_boulevard <- NA

#for each band, subset to nodes that exist as of that band (so that band and all before it)
for(i in 1:length(bands)){
print(i)

if(i > 1){
nodes2 <- nodes[apply(nodes@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
nodes2 <- nodes[nodes@data[,bands[i]]==1,]	
}

#subset the GML object to only the nodes that exist in the subset nodes argument (will have to use the vertex IDs argument)
summary(gml)
head(V(gml)$id)
gml2 <- subgraph(gml, V(gml)$id %in% nodes2@data$osmid)
#summary(gml2)

#need the calculate the vertex ides argument here!
vertex.ids <- as.data.frame(cbind(V(gml2)$id, 1:length(V(gml2)$id)))
vertex.ids[,2] <- as.numeric(vertex.ids[,2] )


#subset the boulevards object to all boulevards that exist as of that band (so that band and all before it)
if(i > 1){
boulevards2 <- boulevards[apply(boulevards@data[,bands[1:i]], MARGIN=1, FUN=max)==1,]
} else{
boulevards2 <- boulevards[boulevards@data[,bands[i]]==1,]	
}
	
#find nodes that intersect the boulevards; these become the "target" nodes
dists1 <- over(nodes2, boulevards2, returnList=F)
nodes3 <- nodes2[is.na(dists1 $u)==F,]

#calculate distances of all nodes in the netwok as of now to these target nodes at the boulevards
nodes.to	<- vertex.ids[vertex.ids[,1] %in% nodes3$osmid,2]   #THIS NEEDS TO BE THE NODES IN NODES 3 (the target nodes)
dists2 <- distances(gml2, to=nodes.to, weights=as.numeric(E(gml2)$length)) #this is the igraph distances function

#subset the nodes to only nodes in *THIS* band; not all past bands
nodes4 <- nodes[nodes@data[,bands[i]]==1,]	
	#for i = 1 these will be equal, but for all others they wont be

#subset the dists object just to the nodes in *THIS* band
dists2a <- dists2[nodes2$osmid %in% nodes4$osmid,]
	#this will be the same when i = 1, but not for other is

#for each one, find the minimum distance
nodes4$dists.min <- apply(dists2a, 1, min, na.rm=T)
	#note: infinity distances are nodes that aren't connected to *any* boulevards
	#setting these to NAs would make the neighborhood seem closer than it is. #assign them the maximum length of nodes built at that time to a boulevard? 
nodes4$dists.min[nodes4$dists.min==Inf] <- max(nodes4$dists.min[nodes4$dists.min!=Inf]) #as these are the really far away ones, should go to the maximum distance away

#now in the main nodes object, assign these to it...
for(j in 1:nrow(nodes4)){
	
	nodes$dist_to_boulevard[nodes$osmid==nodes4$osmid[j]] <- nodes4$dists.min[j]
	
} #end of j data storage loop

} #end of bands i loop
#end the band-level loop

summary(nodes$dist_to_boulevard)


#in a tdat level loop, assign each tdat the average value of its nodes
#for each tdat, calculate the average distance of its nodes (in distance along the street network) to the nearest highway *that existed when the neighborhood itself existed* 

tdat$avg_dist_to_boulevard <- NA
for(i in 1:nrow(tdat)){

tdat$avg_dist_to_boulevard[i] <- mean(nodes@data$dist_to_boulevard[nodes@data$osmid %in% V(gml.sub.list[[i]])$id], na.rm=T)
		
}

tdat$avg_dist_to_boulevard <- tdat$avg_dist_to_boulevard / 1000 #put it in km


save(tdat, file=paste("./boeing_data/tcommunities/", "windhoek-3260", "_tdat_wyears_NEW", ".Rdata", sep="")	)


########### ADDENDUM: 
################ count the total number of time windows by city to report in the paper: #################

rm(list=ls())

city_labels <- c("abuja-combined", "accra-1910", "addis_ababa-5134", "arusha-4800", "bamako-1553", "beira-4521", "brikama-1458", "buchanan-1533", "bujumbura-4078", "cape_town-3268", "gaborone-3587", "gombe-2932", "ibadan-2189", "johannesburg-3673", "kampala-4427", "kara-2026", "khartoum-4335", "kigali-4172", "kinshasa-3209", "lagos-2125", "luanda-3050", "lubumbashi-3756", "malabo-2762", "manzini-4080", "maputo-4220", "maseru-3631", "monrovia-1526", "nairobi-4808", "nakuru-4729", "ndola-3859", "oyo-2191", "pointe_noire-2982", "port_elizabeth-3505", "porto_novo-2101", "touba-1473", "windhoek-3260")

holder <- as.data.frame(matrix(NA, nrow=length(city_labels), ncol=3))
colnames(holder) <- c("city", "time_windows", "n_maps")
holder$city <- city_labels
bands <- list()

for(i in 1:length(city_labels)){
	tdat <- NA
	load(paste("./boeing_data/tcommunities/", city_labels[i], "_tdat_wyears_NEW", ".Rdata", sep=""))
	#15 colnames before the years, then 2 after (average age)
	holder[i,2] <- length(colnames(tdat)) - 18
	holder[i,3] <- length(colnames(tdat)) - 18 - 1
	bands[[i]] <- colnames(tdat)[16: ((ncol(tdat))-3)]
}

sum(holder$n_maps)
#227

mean(holder$time_windows)
#7.305556 #average of 7.3 time windows

bands

